rfazendeiro
Well-known member
- Joined
- Mar 8, 2004
- Messages
- 110
Im having a little problem saving a image to disk. I get the image from a url, and want to save that to disk.
So i just do a HttpWebRequest to the image url, get the stream and convert it to an image and save to disk. This code here is working for jpgs but its does not work with GIF. also there are somethings that would like to change. the code is this:
my problem is that this code does actually work for JPG. But when i try to get a GIF i get an exception when tring to save to disk. Any ideia why?
Exception:
So i just do a HttpWebRequest to the image url, get the stream and convert it to an image and save to disk. This code here is working for jpgs but its does not work with GIF. also there are somethings that would like to change. the code is this:
PHP:
public static String HttpRequest(String serverImgURL, String physicalPath, String extention)
{
String siteImgURL;
HttpWebRequest loHttp = (HttpWebRequest)WebRequest.Create(serverImgURL);
HttpWebResponse loWebResponse = (HttpWebResponse)loHttp.GetResponse();
Encoding enc = Encoding.GetEncoding("ISO-8859-1");
StreamReader loResponseStream = new StreamReader(loWebResponse.GetResponseStream(), enc);
String lcHtml = loResponseStream.ReadToEnd();
loWebResponse.Close();
loResponseStream.Close();
String imageName = serverImgURL.Substring(serverImgURL.LastIndexOf("/") + 1, serverImgURL.Length - serverImgURL.LastIndexOf("/") - 1);
siteImgURL = ConvertToImage(lcHtml, physicalPath, imageName, extention);
return siteImgURL;
}
private static String ConvertToImage(String stringImage, String physicalPath, String imageName, String extention)
{
string imageurl = string.Empty;
if (stringImage.Length > 0)
{
Byte[] bitmapData;
bitmapData = ToByteArray(stringImage);
MemoryStream streamBitmap = new MemoryStream(bitmapData);
Bitmap bitImage = new Bitmap(Image.FromStream(streamBitmap));
String fotoPath;
fotoPath = physicalPath + "\\" + imageName;
if (extention.ToLower() == "jpg")
bitImage.Save(fotoPath, ImageFormat.Jpeg);
else
bitImage.Save(fotoPath, ImageFormat.Gif);
}
return imageurl;
}
public static Byte[] ToByteArray(String StringToConvert)
{
Encoding encoding = Encoding.GetEncoding("ISO-8859-1");
Byte[] ByteArray = encoding.GetBytes(StringToConvert);
return ByteArray;
}
my problem is that this code does actually work for JPG. But when i try to get a GIF i get an exception when tring to save to disk. Any ideia why?
Code:
bitImage.Save(fotoPath, ImageFormat.Gif);
{"A generic error occurred in GDI+."}
Exception:
Code:
System.Runtime.InteropServices.ExternalException was unhandled by user code
Message="A generic error occurred in GDI+."
Source="System.Drawing"
ErrorCode=-2147467259