Web serice to upload image on server

/// web service
/// Upload image on server with the .jpg, .ico, .gif, .bmp, .png formats.
/// Size should be less than 80kb
///
/// /// ///
[WebMethod]
public string UploadImage(byte[] ImgIn, string FileName)
{
MemoryStream ms = null;
Bitmap b = null;
try
{
ms = new MemoryStream(ImgIn);
if (ms.Length <= 80000) // 80kb limit for image to upload
{
b = (Bitmap)Image.FromStream(ms);
if (FileName.ToLower().Contains(".jpeg") || FileName.ToLower().Contains(".jpg"))
b.Save("C:\\" + FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
if (FileName.ToLower().Contains(".gif"))
b.Save("C:\\" + FileName, System.Drawing.Imaging.ImageFormat.Gif);
if (FileName.ToLower().Contains(".bmp"))
b.Save("C:\\" + FileName, System.Drawing.Imaging.ImageFormat.Bmp);
if (FileName.ToLower().Contains(".png"))
b.Save("C:\\" + FileName, System.Drawing.Imaging.ImageFormat.Png);
if (FileName.ToLower().Contains(".ico"))
b.Save("C:\\" + FileName, System.Drawing.Imaging.ImageFormat.Icon);
return "Uploaded successfully.";
}
else
return "Image size should be less than 80kb.";
}
catch (Exception ex)
{
return "Some error occurred, please try again later.";
}
finally
{
if (ms != null)
ms.Dispose();
if (b != null)
b.Dispose();
}
}

// Consuming the web service

localhost.ImageUpload obj = new localhost.ImageUpload();
FileStream fs = new FileStream(FileUpload1.PostedFile.FileName, FileMode.OpenOrCreate, FileAccess.Read);
Byte[] img = new Byte[fs.Length];
fs.Read(img, 0, Convert.ToInt32(fs.Length));      
lblMsg.Text = obj.UploadImage(img, FileUpload1.FileName);

Comments

Popular posts from this blog

How to get motherboard serial number, Processor ID, VolumeSerialNumber using C#?

Fiserv Interview Questions.

AngularJs - Simple Example