473,413 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

I don't know why this fixes this... any ideas? (System.Drawing.Image.FromStream "parameter is not valid")

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[i].ItemArray[1];
//b.ToString()
ms = new MemoryStream();
ms.Write(b, 0, b.Length);
Image.GetThumbnailImageAbort callback = new
System.Drawing.Image.GetThumbnailImageAbort(Thumbn ailCallback);
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[i].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(Thumbn ailCallback);
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.

Oct 27 '06 #1
7 4755
It looks like you have left the stream at the end... if you use the
overloaded ctor you don't have to worry about this:

ms = new MemoryStream(b);
// snip
Image img = Image.FromStream(ms);

Marc
Oct 27 '06 #2

Marc Gravell wrote:
It looks like you have left the stream at the end... if you use the
overloaded ctor you don't have to worry about this:

ms = new MemoryStream(b);
// snip
Image img = Image.FromStream(ms);

Marc
Thanks...

I only showed one version. I tried all three (ms),
(ms,false/true),(ms,false/true,false/true) with no difference.

Oct 27 '06 #3


<bo****@gmail.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
>
Marc Gravell wrote:
>It looks like you have left the stream at the end... if you use the
overloaded ctor you don't have to worry about this:

ms = new MemoryStream(b);
// snip
Image img = Image.FromStream(ms);

Marc

Thanks...

I only showed one version. I tried all three (ms),
(ms,false/true),(ms,false/true,false/true) with no difference.
What I believe Marc meant was to use the MemoryStream's overloaded
constructor that takes a byte-array as a parameter. What you are doing is
using the default constructor and then writing the byte-array to the stream.
In other words, instead of writing the byte-array to the stream, directly,
you can just pass the byte-array into the constructor of the MemoryStream
and it will automatically be written by the MemoryStream without any extra
processing on your part:

ms = new MemoryStream(b); // This line here.
....
Image img = Image.FromStream(ms); // This just shows passing the stream
after it's created.
HTH,
Mythran
Oct 27 '06 #4

Mythran wrote:
<bo****@gmail.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...

Marc Gravell wrote:
It looks like you have left the stream at the end... if you use the
overloaded ctor you don't have to worry about this:

ms = new MemoryStream(b);
// snip
Image img = Image.FromStream(ms);

Marc
Thanks...

I only showed one version. I tried all three (ms),
(ms,false/true),(ms,false/true,false/true) with no difference.

What I believe Marc meant was to use the MemoryStream's overloaded
constructor that takes a byte-array as a parameter. What you are doing is
using the default constructor and then writing the byte-array to the stream.
In other words, instead of writing the byte-array to the stream, directly,
you can just pass the byte-array into the constructor of the MemoryStream
and it will automatically be written by the MemoryStream without any extra
processing on your part:

ms = new MemoryStream(b); // This line here.
...
Image img = Image.FromStream(ms); // This just shows passing the stream
after it's created.
HTH,
Mythran
I see... wrong ctor :)
I tried this:
b = (byte[])dt.Rows[i].ItemArray[1];

ms = new MemoryStream(b);

Image.GetThumbnailImageAbort callback = new
System.Drawing.Image.GetThumbnailImageAbort(Thumbn ailCallback);
IntPtr callbackData = new IntPtr(0);


try
{

Image img = Image.FromStream(ms);
...do stuff here with image...
}

to no avail...

in fact using the ms = new MemoryStream(b) instead of ms.Write(b, 0,
b.Length) using the current StreamWiter fix causes a memory stream not
expandable error.
Any ideas why the streamwriter fixes this currently?
Thanks.

Oct 27 '06 #5


<bo****@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
>
Mythran wrote:
><bo****@gmail.comwrote in message
news:11**********************@m7g2000cwm.googlegr oups.com...
>
Marc Gravell wrote:
It looks like you have left the stream at the end... if you use the
overloaded ctor you don't have to worry about this:

ms = new MemoryStream(b);
// snip
Image img = Image.FromStream(ms);

Marc

Thanks...

I only showed one version. I tried all three (ms),
(ms,false/true),(ms,false/true,false/true) with no difference.

What I believe Marc meant was to use the MemoryStream's overloaded
constructor that takes a byte-array as a parameter. What you are doing
is
using the default constructor and then writing the byte-array to the
stream.
In other words, instead of writing the byte-array to the stream,
directly,
you can just pass the byte-array into the constructor of the MemoryStream
and it will automatically be written by the MemoryStream without any
extra
processing on your part:

ms = new MemoryStream(b); // This line here.
...
Image img = Image.FromStream(ms); // This just shows passing the stream
after it's created.
HTH,
Mythran

I see... wrong ctor :)
I tried this:
b = (byte[])dt.Rows[i].ItemArray[1];

ms = new MemoryStream(b);

Image.GetThumbnailImageAbort callback = new
System.Drawing.Image.GetThumbnailImageAbort(Thumbn ailCallback);
IntPtr callbackData = new IntPtr(0);


try
{

Image img = Image.FromStream(ms);
...do stuff here with image...
}

to no avail...

in fact using the ms = new MemoryStream(b) instead of ms.Write(b, 0,
b.Length) using the current StreamWiter fix causes a memory stream not
expandable error.
Any ideas why the streamwriter fixes this currently?
Thanks.
Unfortunately, without me seeing all of the involved code, I am unable to
determine the cause (unless I'm just not seeing it). Hope someone else can
chime in here and give a hand if they see it.

Mythran
Oct 27 '06 #6
bo****@gmail.com wrote:
Mythran wrote:
<bo****@gmail.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
>
Marc Gravell wrote:
>It looks like you have left the stream at the end... if you use the
>overloaded ctor you don't have to worry about this:
>>
>ms = new MemoryStream(b);
>// snip
>Image img = Image.FromStream(ms);
>>
>Marc
>
Thanks...
>
I only showed one version. I tried all three (ms),
(ms,false/true),(ms,false/true,false/true) with no difference.
>
What I believe Marc meant was to use the MemoryStream's overloaded
constructor that takes a byte-array as a parameter. What you are doing is
using the default constructor and then writing the byte-array to the stream.
In other words, instead of writing the byte-array to the stream, directly,
you can just pass the byte-array into the constructor of the MemoryStream
and it will automatically be written by the MemoryStream without any extra
processing on your part:

ms = new MemoryStream(b); // This line here.
...
Image img = Image.FromStream(ms); // This just shows passing the stream
after it's created.
HTH,
Mythran

I see... wrong ctor :)
I tried this:
b = (byte[])dt.Rows[i].ItemArray[1];

ms = new MemoryStream(b);

Image.GetThumbnailImageAbort callback = new
System.Drawing.Image.GetThumbnailImageAbort(Thumbn ailCallback);
IntPtr callbackData = new IntPtr(0);


try
{

Image img = Image.FromStream(ms);
...do stuff here with image...
}

to no avail...

in fact using the ms = new MemoryStream(b) instead of ms.Write(b, 0,
b.Length) using the current StreamWiter fix causes a memory stream not
expandable error.
Any ideas why the streamwriter fixes this currently?
In your initial version, using only the MemoryStream without the
StreamWriter, perhaps you need to seek back to the beginning of the
stream before using it in FromStream:

ms.Seek(0, SeekOrigin.Begin);
Image img = Image.FromStream(ms);

I think that's what Marc may have been referring to. Perhaps using the
StreamWriter does this.

Just a guess

Oct 27 '06 #7
Chris Dunaway wrote:
bo****@gmail.com wrote:
Mythran wrote:
<bo****@gmail.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...

Marc Gravell wrote:
It looks like you have left the stream at the end... if you use the
overloaded ctor you don't have to worry about this:
>
ms = new MemoryStream(b);
// snip
Image img = Image.FromStream(ms);
>
Marc

Thanks...

I only showed one version. I tried all three (ms),
(ms,false/true),(ms,false/true,false/true) with no difference.

>
What I believe Marc meant was to use the MemoryStream's overloaded
constructor that takes a byte-array as a parameter. What you are doing is
using the default constructor and then writing the byte-array to the stream.
In other words, instead of writing the byte-array to the stream, directly,
you can just pass the byte-array into the constructor of the MemoryStream
and it will automatically be written by the MemoryStream without any extra
processing on your part:
>
ms = new MemoryStream(b); // This line here.
...
Image img = Image.FromStream(ms); // This just shows passing the stream
after it's created.
>
>
HTH,
Mythran
I see... wrong ctor :)
I tried this:
b = (byte[])dt.Rows[i].ItemArray[1];

ms = new MemoryStream(b);

Image.GetThumbnailImageAbort callback = new
System.Drawing.Image.GetThumbnailImageAbort(Thumbn ailCallback);
IntPtr callbackData = new IntPtr(0);


try
{

Image img = Image.FromStream(ms);
...do stuff here with image...
}

to no avail...

in fact using the ms = new MemoryStream(b) instead of ms.Write(b, 0,
b.Length) using the current StreamWiter fix causes a memory stream not
expandable error.
Any ideas why the streamwriter fixes this currently?

In your initial version, using only the MemoryStream without the
StreamWriter, perhaps you need to seek back to the beginning of the
stream before using it in FromStream:

ms.Seek(0, SeekOrigin.Begin);
Image img = Image.FromStream(ms);

I think that's what Marc may have been referring to. Perhaps using the
StreamWriter does this.

Just a guess
I was thinking something along those lines as well. The problem with
that is that it works just fine almost all the time. Only 11 out 100
images failed.

Thanks...

Oct 27 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dante | last post by:
Just a thought: what if you posted the W3C's "Valid XHTML" images on a webpage that doesn't even use XHTML? Or with a Valid CSS thing. What could the W3C do to you anyway? Maybe I'll do that just...
11
by: Florian Loitsch | last post by:
I'm currently writing a JS->Scheme compiler (which, using Bigloo, automatically yields a JS->C, JS->JVM, JS->.NET compiler), and have a question concerning the function-parameters: According to...
2
by: John A Grandy | last post by:
Is there something special in XML about the name "Parameter" ... I am not able to XPATH query for nodes named "Parameter" <Root> <Category CategoryID="1"> <Elements> <Element...
40
by: Dave Hansen | last post by:
Please note crosspost. Often when writing code requiring function pointers, it is necessary to write functions that ignore their formal parameters. For example, a state machine function might...
11
by: Roy Lawson | last post by:
I have no idea what is going on here. I wrote a simple application in VB.NET to generate a Crystal Report, and I am now trying to move it to ASP.NET with Crstal Enterprise. I wish I could tell...
8
by: Charles | last post by:
I do not understand why I am getting a "Specified cast is not valid" error, since it has worked before. Something has changed and I am not really sure what it could be. I am looking for something...
4
by: Manuel | last post by:
I think I can't find this on google/books because is soooooooo basic... This function assign a listner pointer to an image widget: ------------------------------------------------------ void...
2
by: Nemisis | last post by:
Hi, Is it possible to pass in an object and parameter into a function and return it as a string. i.e. To make a call to the function i would put the following Dim str as String =...
7
by: =?Utf-8?B?Sm9hY2hpbQ==?= | last post by:
I have an image which I'm trying to save using my_image.Save(some_path, System.Drawing.Imaging.ImageFormat.Path); and then I get the error "Parameter is not valid". What could be the reason...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.