วันพุธที่ 16 พฤษภาคม พ.ศ. 2555

Photo Gallery Wall

An attractive image gallery containing 16 images. Images and their captions (if configured) can be viewed by clicking. If no images are selected, the gallery will automatically switch to slideshow mode after 10 seconds. Includes a how-to document that provides an overview of the creation process using Blend and detailed description of functionality! View Download (706K ZIP)

Create image gallery,store picture in SQL Server 2005 Express

How do the user add picture?
I have an example here like using Upload
C#
private void Btn_AddAtt_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog d = new OpenFileDialog();
d.Filter = "Image (*.jpg, *.png)|*.jpg;*.png";
d.Multiselect = true;
if (d.ShowDialog() == true)
{
foreach (FileDialogFileInfo f in d.SelectedFiles)
{
Stream s = f.OpenRead();
byte[] fileBytes = new byte[s.Length];
int byteCount = s.Read(fileBytes, 0, (int)s.Length);
string fileContent = Convert.ToBase64String(fileBytes);
s.Close();
//Upload here
// mydata.UploadFileAsync(f.Name, fileContent);
}
}
}
mydata.LoadImage_Completed(....)
{
byte[] m;
Stream b;
if (e.Error == null)
{
List<ImageFile> k = e.Result as List<ImageFile>;
//using for-looping with int i to get the content and change it back to Image
m = Convert.FromBase64String(k[ i].Content);
b.WriteByte(m);
BitmapImage k = new BitmapImage();
k.SetSource(b);
Image mypic = new Image();
mypic.Source = k;
LayoutRoot.Children.Add(mypic);
}
}
In the WCF
public void UploadFile(string fileName, string text)
{
//you have now the text with Base64 Configuration, you can insert it into your SQL database
// next time you can get it back by loadImage
}
public class ImageFile
{
public string Name { get; set; }
public string Content { get; set; }
}
public string LoadImage()
{
//you can now find all the Row in the Database and send back the Name and Text
//you may return List<ImageFile>
}
I didnt try them all in VS but the upload part should work very well

Silverlight Tip How to Dynamically Load and Display Images

How to Dynamically Load and Display Images

Loading images in Silverlight is fairly straight forward. The first step is to create what’s called a Uniform Resource Identifier (URI). The URI is essentially a string that points to a resource. The resource can be locally in the project or out on the Internet. Images that are loaded locally must be first added to your Visual Studio project.

Image image = new Image();
Uri uri = new Uri("images/myImage.png", UriKind.Relative);
ImageSource img = new System.Windows.Media.Imaging.BitmapImage(uri);
image.SetValue(Image.SourceProperty, img)
Now, to display the image you will need to add it to the Children of an element that you have declared in your XAML. For example, let’s say you have created a Canvas object in your Page.xaml under the parent Grid object. Using the x:Name tag, give it the name “Map”:
<Grid x:Name="MainGrid">
  <Canvas x:Name="Map">
   </Canvas>
</Grid>
Now, back in your Page.xaml.cs file you can dynamically add the image you just created to the canvas object like this:
Map.Children.Add(image);
Btw, declaring an image in your XAML works the same way. Example:
<Image Source="images/MyImage.png"></Image>

Creating New Database Server in the Azure Portal

Introduction:


In this article we are going to see how to create a new database server in the Azure Management portal.

Overview:


Database servers are created for each subscription based on the level of the registered account. Each subscription can contain multiple logical Azure database servers. Each server is created based on the region selected at the time of configuration of the server. Once the server is created we can see a read only Master database is created automatically with some login credentials. We can create our own database after the servers are configured correctly based on our requirement.

Steps:


Log in to the Azure portal using the link below. You will see the screen look similar to below.
http://www.microsoft.com/windowsazure/

image

Login to the portal using your Microsoft Windows Live credentials with Azure credentials to the management portal and you will see the screen as shown in the screen below:
SEE MORE

ResizeImage Spec. MaxSize

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

public static void ResizeImage(string fileNameIn, string fileNameOut, int maxWidthOrHeight)
{
// Open the image
Bitmap bmpSource = new Bitmap(fileNameIn);

Size originalSize = bmpSource.Size;
Size newSize = new Size(0, 0);

bmpSource.Dispose();

decimal maxHeightDecimal = Convert.ToDecimal(maxWidthOrHeight);
decimal maxWidthDecimal = Convert.ToDecimal(maxWidthOrHeight);

decimal resizeFactor = 0;

if (originalSize.Height > originalSize.Width)
{
// Portrait
resizeFactor = Convert.ToDecimal(Decimal.Divide(originalSize.Height, maxHeightDecimal));
newSize.Height = maxWidthOrHeight;
newSize.Width = Convert.ToInt32(originalSize.Width / resizeFactor);
}
else
{
// Lanscape
resizeFactor = Convert.ToDecimal(Decimal.Divide(originalSize.Width, maxWidthDecimal));
newSize.Height = Convert.ToInt32(originalSize.Height / resizeFactor);
newSize.Width = maxWidthOrHeight;
}

Bitmap mySourceBitmap = null;
Bitmap myTargetBitmap = null;
Graphics myGraphics = null;

try
{
mySourceBitmap = new Bitmap(fileNameIn);

int newWidth = newSize.Width;
int newHeight = newSize.Height;

myTargetBitmap = new Bitmap(mySourceBitmap, newWidth, newHeight);

myGraphics = Graphics.FromImage(myTargetBitmap);

myGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
mySourceBitmap.Dispose();

// Save th new image
myTargetBitmap.Save(fileNameOut, ImageFormat.Jpeg);
}
catch (Exception)
{
throw;
}
finally
{
if (myGraphics != null) myGraphics.Dispose();
if (mySourceBitmap != null) mySourceBitmap.Dispose();
if (myTargetBitmap != null) myTargetBitmap.Dispose();
}
}

Http Handlers to handle Images

HTTP handlers implement the following two methods:
1) ProcessRequest: called to process http requests and
2) IsReusable which indicates whether this instance of http handler can be reused for fulfilling another requests of the same type.

In your website Add new Item Generic Handler and name it as ImageHandler.ashx

Here is ImageHandler.ashx file

<%@ WebHandler Language="C#" Class="ImageHandler" %>

using System;
using System.Web;
using System.IO;

public class ImageHandler : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
string sImageFileName = "";
int iThumbSize = 0;
decimal dHeight, dWidth, dNewHeight, dNewWidth;

sImageFileName = context.Request.QueryString["img"];
iThumbSize = Convert.ToInt32(context.Request.QueryString["sz"]);

System.Drawing.Image objImage = System.Drawing.Bitmap.FromFile(System.Web.HttpContext.Current.Server.MapPath("Image Path" + sImageFileName));
if (sImageFileName != null)
{
if (iThumbSize == 1)
{

dHeight = objImage.Height;
dWidth = objImage.Width;
dNewHeight = 120;
dNewWidth = dWidth * (dNewHeight / dHeight);
objImage = objImage.GetThumbnailImage((int)dNewWidth, (int)dNewHeight, new System.Drawing.Image.GetThumbnailImageAbort(callback), new IntPtr());
}
if (iThumbSize == 2)
{

dHeight = objImage.Height;
dWidth = objImage.Width;
dNewHeight = 200;
dNewWidth = dWidth * (dNewHeight / dHeight);
objImage = objImage.GetThumbnailImage((int)dNewWidth, (int)dNewHeight, new System.Drawing.Image.GetThumbnailImageAbort(callback), new IntPtr());
}

MemoryStream objMemoryStream = new MemoryStream();
objImage.Save(objMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] imageContent = new byte[objMemoryStream.Length];
objMemoryStream.Position = 0;
objMemoryStream.Read(imageContent, 0, (int)objMemoryStream.Length);
context.Response.ContentType = "image/jpeg";
context.Response.BinaryWrite(imageContent);
}
}


private bool callback()
{
return true;
}
public bool IsReusable
{
get
{
return false;
}
}

}

Following is the way to write it..

<img id="Image1" src='../ImageHandler.ashx?img=ImageName&sz=1' />

Upload Image and Resize


1.  using System.IO;
2.  using System.Web;

3.  public static string UploadImage(string newFileName, string directory, FileUpload fileUpload, int imageSize, bool overwrite)
4.  {
5.  if (fileUpload.HasFile)
6.  {
7.  string oldFileName = fileUpload.FileName;
8.  DirectoryInfo dirTempInfo = new DirectoryInfo(HttpContext.Current.Request.PhysicalApplicationPath + "Temp");
9.  if (!dirTempInfo.Exists) dirTempInfo.Create();
10. string tempPath = dirTempInfo.FullName + @"\" + oldFileName;
11. fileUpload.SaveAs(tempPath);
12. FileInfo tempInfo = new FileInfo(tempPath);
13. string extension = tempInfo.Extension;
14. if (newFileName == "") newFileName = tempInfo.Name.Substring(0, tempInfo.Name.IndexOf(extension));
15. string newFullName = newFileName + extension;
16. DirectoryInfo dirInfo = new DirectoryInfo(HttpContext.Current.Request.PhysicalApplicationPath + directory);
17. if (!dirInfo.Exists) dirInfo.Create();
18. string filePath = dirInfo.FullName + @"\" + newFullName;
19. FileInfo fileInfo = new FileInfo(filePath);
20. if (!fileInfo.Exists || overwrite)
21. {
22. if (fileInfo.Exists) fileInfo.Delete();
23. if (imageSize > 0)
24. {
25. ResizeImage(tempPath, filePath, imageSize);
26. tempInfo.Delete();
27. }
28. else tempInfo.MoveTo(filePath);
29. }
30. else tempInfo.Delete();
31. return directory + "/" + newFullName;
32. }
33. else return "";
34. }

35.  เราต้องกำหนดขนาดไฟล์สูงสุดที่ upload หน่วยเป็น KB ได้ที่ web.config แต่เท่าที่ทำแม้กำหนดไฟล์ใหย๋มากๆ ก็ส่งไฟล์ขนาดมากๆขึ้นไปไม่ได้อยู่ดีเพราะ Timeout return error

36. <system.web>
<httpRuntime maxRequestLength="4096"/>
< /system.web>