B
bookon
I was running into the System.Drawing.Image.FromStream "parameter is
not valid" on some of the images I was retrieving from a blob column in
Sql Server. I thought there were corrupt images as almost all worked
(all are gifs), and only a few broke when this line ran: Image img =
Image.FromStream(ms); here is the original code:
b = (byte[])dt.Rows.ItemArray[1];
//b.ToString()
ms = new MemoryStream();
ms.Write(b, 0, b.Length);
Image.GetThumbnailImageAbort callback = new
System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
IntPtr callbackData = new IntPtr(0);
try
{
Image img = Image.FromStream(ms);
...do something with image...
}
I created a StringBulider object with the intention of examining the
contents of "sb" as a file, but simply the act of doing this fixed
the problem and ALL images now work fine.
StringBuilder sb = new StringBuilder();
b = (byte[])dt.Rows.ItemArray[1];
//b.ToString()
foreach (Byte bt in b)
{
sb.Append(bt);
}
ms = new MemoryStream();
ms.Write(b, 0, b.Length);
StreamWriter sw = new StreamWriter(ms);
sw.Write(sb.ToString());
Image.GetThumbnailImageAbort callback = new
System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
IntPtr callbackData = new IntPtr(0);
try
{
Image img = Image.FromStream(ms, false, false);
...do something with image...
}
As you can see merely the act of passing 'ms' as a parameter to a
StreamWriter fixes it. OR The steps I take to be able to do, such as
the foreach loop or the sw.Write(). "ms" appears the same before and
after being passed.
Any ideas on why this fixes my problem would be great.
Thanks.
not valid" on some of the images I was retrieving from a blob column in
Sql Server. I thought there were corrupt images as almost all worked
(all are gifs), and only a few broke when this line ran: Image img =
Image.FromStream(ms); here is the original code:
b = (byte[])dt.Rows.ItemArray[1];
//b.ToString()
ms = new MemoryStream();
ms.Write(b, 0, b.Length);
Image.GetThumbnailImageAbort callback = new
System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
IntPtr callbackData = new IntPtr(0);
try
{
Image img = Image.FromStream(ms);
...do something with image...
}
I created a StringBulider object with the intention of examining the
contents of "sb" as a file, but simply the act of doing this fixed
the problem and ALL images now work fine.
StringBuilder sb = new StringBuilder();
b = (byte[])dt.Rows.ItemArray[1];
//b.ToString()
foreach (Byte bt in b)
{
sb.Append(bt);
}
ms = new MemoryStream();
ms.Write(b, 0, b.Length);
StreamWriter sw = new StreamWriter(ms);
sw.Write(sb.ToString());
Image.GetThumbnailImageAbort callback = new
System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
IntPtr callbackData = new IntPtr(0);
try
{
Image img = Image.FromStream(ms, false, false);
...do something with image...
}
As you can see merely the act of passing 'ms' as a parameter to a
StreamWriter fixes it. OR The steps I take to be able to do, such as
the foreach loop or the sw.Write(). "ms" appears the same before and
after being passed.
Any ideas on why this fixes my problem would be great.
Thanks.