I'm trying to get an image from an URL but when I save it to a file it is half of the actual image! I have searched many sites and solutions like HttpWebRequest.BeginGetResponse because I thought it is because I must buffer the data but it did not work. I don't know which part of my code is wrong and making this problem!:(
This is the code which retrieve image from URL:
public static Bitmap GetImageFromUrl(string url)
{
string RefererUrl = string.Empty;
int TimeoutMs = 22 * 1000;
string requestAccept = "*/*";
string UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7";
Bitmap img = null;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.UserAgent = UserAgent;
request.Timeout = TimeoutMs;
request.ReadWriteTimeout = TimeoutMs * 6;
request.Accept = requestAccept;
if (!string.IsNullOrEmpty(RefererUrl))
{
request.Referer = RefererUrl;
}
try
{
WebResponse wResponse = request.GetResponse();
using (HttpWebResponse response = wResponse as HttpWebResponse)
{
Stream responseStream = response.GetResponseStream();
img = new Bitmap(responseStream);
response.Close();
}
}
catch (Exception)
{
}
return img;
}
This the code which saves the image in File:
byte[] imgBytes = tile.Image;
using (MemoryStream ms = new MemoryStream(imgBytes))
{
using (Image img = Image.FromStream(ms))
{
ms.Dispose();
Bitmap tempBmp = new Bitmap(img);
img.Dispose();
string activeDir = Environment.CurrentDirectory;
string newPath = System.IO.Path.Combine(activeDir, "Images");
System.IO.Directory.CreateDirectory(newPath);
newPath = System.IO.Path.Combine(newPath, tile.TileType.ToString());
System.IO.Directory.CreateDirectory(newPath);
newPath = System.IO.Path.Combine(newPath, tile.Zoom.ToString());
System.IO.Directory.CreateDirectory(newPath);
newPath = System.IO.Path.Combine(newPath, tile.X.ToString());
System.IO.Directory.CreateDirectory(newPath);
newPath = System.IO.Path.Combine(newPath, tile.Y.ToString());
System.IO.Directory.CreateDirectory(newPath);
string newFileName = "tile.png";
newPath = System.IO.Path.Combine(newPath, newFileName);
tempBmp.Save(newPath, ImageFormat.Png);
tempBmp.Dispose();
You should not call
Dispose()
onms
andimg
objects until you have saved it to the file at the end.They are even declared in their own
using
sections, so you don't need to callDispose()
on them at all, because it is automatically disposed at the exit of each used block (this is really the main reason of usingusing
in the first place!).Your code seems overcomplicated, do you really need read image and then resave it? Why can't you just save downloaded image directly, like this:
You should of course split it into methods and set proper return values