473,890 Members | 1,885 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

I was running into the System.Drawing. Image.FromStrea m "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.FromStrea m(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.GetThumbn ailImageAbort callback = new
System.Drawing. Image.GetThumbn ailImageAbort(T humbnailCallbac k);
IntPtr callbackData = new IntPtr(0);

try
{

Image img = Image.FromStrea m(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.ToS tring());
Image.GetThumbn ailImageAbort callback = new
System.Drawing. Image.GetThumbn ailImageAbort(T humbnailCallbac k);
IntPtr callbackData = new IntPtr(0);

try
{

Image img = Image.FromStrea m(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 4833
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.FromStrea m(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.FromStrea m(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.c omwrote in message
news:11******** **************@ m7g2000cwm.goog legroups.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.FromStrea m(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.FromStrea m(ms); // This just shows passing the stream
after it's created.
HTH,
Mythran
Oct 27 '06 #4

Mythran wrote:
<bo****@gmail.c omwrote in message
news:11******** **************@ m7g2000cwm.goog legroups.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.FromStrea m(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.FromStrea m(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.GetThumbn ailImageAbort callback = new
System.Drawing. Image.GetThumbn ailImageAbort(T humbnailCallbac k);
IntPtr callbackData = new IntPtr(0);


try
{

Image img = Image.FromStrea m(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.c omwrote in message
news:11******** **************@ e3g2000cwe.goog legroups.com...
>
Mythran wrote:
><bo****@gmail. comwrote in message
news:11******* *************** @m7g2000cwm.goo glegroups.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.FromStrea m(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.FromStrea m(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.GetThumbn ailImageAbort callback = new
System.Drawing. Image.GetThumbn ailImageAbort(T humbnailCallbac k);
IntPtr callbackData = new IntPtr(0);


try
{

Image img = Image.FromStrea m(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.co m wrote:
Mythran wrote:
<bo****@gmail.c omwrote in message
news:11******** **************@ m7g2000cwm.goog legroups.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.FromStrea m(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.FromStrea m(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.GetThumbn ailImageAbort callback = new
System.Drawing. Image.GetThumbn ailImageAbort(T humbnailCallbac k);
IntPtr callbackData = new IntPtr(0);


try
{

Image img = Image.FromStrea m(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.Begi n);
Image img = Image.FromStrea m(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.co m wrote:
Mythran wrote:
<bo****@gmail.c omwrote in message
news:11******** **************@ m7g2000cwm.goog legroups.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.FromStrea m(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.FromStrea m(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.GetThumbn ailImageAbort callback = new
System.Drawing. Image.GetThumbn ailImageAbort(T humbnailCallbac k);
IntPtr callbackData = new IntPtr(0);


try
{

Image img = Image.FromStrea m(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.Begi n);
Image img = Image.FromStrea m(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
12419
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 to spite them.
11
2490
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 the spec (10.1.6) the activation-object already contains the the parameters-object when the variables are instantiated (10.1.3). The question is now: does a parameter "parameters" overwrite the "parameters"-object or not? All three implementations...
2
2763
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 ElementID="1"></Element> <Element ElementID="2"></Element> <Element ElementID="3"></Element> </Elements>
40
7931
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 take a status input, but a certain error-handling state might ignore it: typedef void (*State_Fn)(uint8_t); void error_state(uint8_t status)
11
3133
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 you how the report works...but I can't even get past connecting to the database :-) I use the typical: Dim oConnection As New SqlConnection(strCon) Dim cmdText As String = "SELECT * FROM viewLogin WHERE " & _
8
1810
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 along the lines of did you check this, have you did this, or this is the wrong way of doing this. I can access the CColPositions and the CPosition class, but when I try to use the two classes in a For...Next loop I get the error message above. ...
4
12140
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 image::setListener(AbstractListener *listener) { this->listener = listener; } ------------------------------------------------------
2
1463
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 = MyTestFunction(myObject.Parameter)
7
21597
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 for this error? I know I can store it as a Png file.
0
9975
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9812
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10799
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10446
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9614
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8004
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7154
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6032
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4655
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.