Saving images directly from a web url

For saving image directly from a web url, firstly, you need to read the bytes of image and then have to write the bytes on the desired location. You can do this by coding directly in your codebehind page or you may write a function in a class(.cs file) and place that class in App_Code folder.
Here, I am using second option i.e. writing function in .cs file in App_Code folder and using it on the every desired page.


Step1) Make a file named ReadingImage.cs in App_Code(you may name it according to your wish)
Add these functions in this file


public static string UploadImageFromUrl(string file, string UploadPath)
{
string sReturn = "";
if (file != "")
{
string[] ssd = file.Split('/');
string sFileName = ssd[ssd.Length - 1].ToString();//getting name of image
string sPath = UploadPath + "t_" + sFileName;//source path(temporary) for a temprory image
WriteBytesToFile(sPath, GetBytesFromUrl(file));//writing bytes to temporary image
sReturn = sFileName;
string dPath = UploadPath + sFileName;//destination path(original)
ResizeImage(sPath, dPath, 100, 100);//resizing image, here i choose 100px width, 100px height
}
return sReturn;
}
public static byte[] GetBytesFromUrl(string url)
{
byte[] b;
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
WebResponse myResp = myReq.GetResponse();
Stream stream = myResp.GetResponseStream();
using (BinaryReader br = new BinaryReader(stream))
{
b = br.ReadBytes(500000);
br.Close();
}
myResp.Close();
return b;
}

public static void WriteBytesToFile(string fileName, byte[] content)
{
FileStream fs = new FileStream(fileName, FileMode.Create);
BinaryWriter w = new BinaryWriter(fs);
try
{
w.Write(content);
}
finally
{
fs.Close();
w.Close();
}

}


public static void ResizeImage(string imagesrc, string imagedestination, Int32 Targetw, Int32 Targeth)
{
string imgsrc = imagesrc;
string imgdestinationpath = imagedestination;
System.Drawing.Image img = System.Drawing.Image.FromFile(imgsrc);

Int32 initialwidth = img.Width;
Int32 initialheight = img.Height;

Int32 thumbimgwidth = Targetw;
Int32 thumbimgheight = Targeth;

if (initialwidth > initialheight)
{
thumbimgwidth = Targetw;
thumbimgheight = (initialheight * Targetw) / initialwidth;
}
else
{
thumbimgheight = Targeth;
thumbimgwidth = (initialwidth * Targeth) / initialheight;

}
Bitmap bitmap = new Bitmap(thumbimgwidth, thumbimgheight);
Graphics gr = System.Drawing.Graphics.FromImage(bitmap);
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
gr.DrawImage(img, 0, 0, thumbimgwidth, thumbimgheight);
bitmap.Save(imgdestinationpath);
bitmap.Dispose();
img.Dispose();
File.Delete(imgsrc);
}


Step2) Calling this function in your codebehind.


string imageName = ReadingImage.UploadImageFromUrl(WebUrl, PhysicalPathOfDestinationFolder);
// example of destination folder path D:\WebsiteName\images\ .Do not forget to add last backslash(\) in physical path.


So, This statement will save the image file from WebUrl to the given destination location and finally return the image name. This image name can be further used to save in the database.

0 comments:

Post a Comment