 |
|
|
Hi Folk..your code works in .net 2.0 or .net 1.1 .. but it doesnt work in .net 3.5 can you tell me how to go about with this...
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
The Image.FromStream method defines an exception:
The stream does not have a valid image format-or-stream is null.
I am trying to convert an arbitrary byte array (an encryption key) to an image (for safer storage). I keep getting the error 'Parameter is not valid' which suggest to me that my arbitrary byte array is not a valid image format.
I am concluding that this method only works when you convert an image to a byte array and back, not when you start with an arbitrary byte array.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Aye, that's true. You must remember that the data contained in a byte array derived from an image is not merely bytes that get translated into pixel colors arbitrarily. The byte array also must contain header information and be internally formatted according to one of the useable image types (GIF, JPEG, etc...).
If you just send an arbitrary byte array, the chances that it will happen to have the correct format to let it be turned into an image are extremely remote. You might be able to pull it off by adding header and other needed information into the byte array, but that could be complex and still might not work.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I have to store icon files into database. System.Drawing.Imaging.ImageFormat.Icon does not work. So I used .Bmp instead & the first method works fine.
first method:
private byte[] ConvertImageToByte(Image image) { MemoryStream ms = new MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); return ms.ToArray(); }
second method:
private Image ConvertByteToImage(byte[] byteImage) { MemoryStream ms = new MemoryStream(byteImage); Image newImage = Image.FromStream(ms); return newImage; }
But the above code throws exception for second method saying invalid arguments at second line. Is there any particular reason. Please let me know.
Thanks in
-- modified at 18:39 Thursday 22nd November, 2007
|
| Sign In·View Thread·PermaLink | 5.00/5 (3 votes) |
|
|
|
 |
|
|
I am getting the same error when trying to convert a byte array to an image. At the line you have indicated, I get the error: 'Parameter is not valid.' Any thoughts on why?
|
| Sign In·View Thread·PermaLink | 3.00/5 (2 votes) |
|
|
|
 |
|
|
Same. I'm getting the impression that there must be a way to load a particular encoding, because saving the image out as a TIFF and then reloading and converting the stream (for testing) is giving this same exception. Any ideas would be greatly appreciated.
- Lance 'ji' May
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
Very good convert to Binary, it was just what I was looking for.
Here is some addtional code that will use a page to render and image into the Image control from another page.
Image1.ImageUrl = "RenderImage.aspx";
** The Code Below is RenderImage.aspx - This will resize the image and render it to the browser. The calling page will now have the image.
private void ResizeAndRenderImage(System.Drawing.Image image, int height, int width, string contentType) { //Orignal Image System.Drawing.Image FullSizeImg = image; //Dummy callback, for what I have no idea System.Drawing.Image.GetThumbnailImageAbort dummyCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
//The resized image System.Drawing.Image ThumbNailImg = FullSizeImg.GetThumbnailImage(width, height, dummyCallBack, IntPtr.Zero);
System.Drawing.Imaging.ImageFormat ImgFormat; ImgFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
switch (contentType.Trim().ToLower()) { case "image/pjpeg": ImgFormat = System.Drawing.Imaging.ImageFormat.Jpeg; break; case "image/x-png": ImgFormat = System.Drawing.Imaging.ImageFormat.Png; break; case "image/gif": ImgFormat = System.Drawing.Imaging.ImageFormat.Gif; break; } //Convert the image to byte and set image type MemoryStream Imagems = new MemoryStream(); ThumbNailImg.Save(Imagems, ImgFormat);
//Render the image so the Image on the calling page gets it. Response.Clear(); Response.ContentType = ContentType; Response.BinaryWrite(Imagems.ToArray());
FullSizeImg.Dispose(); ThumbNailImg.Dispose(); } public bool ThumbnailCallback() { return false; }
Just contact me if you want all the code.
Use my contact page at www.SURFThru.com
.
|
| Sign In·View Thread·PermaLink | 3.50/5 (2 votes) |
|
|
|
 |
|
|
 |
|
|
 |
|
|
public static byte[] ConvertImageToByteArray(Image imageToConvert) { using (MemoryStream ms = new MemoryStream()) { imageToConvert.Save(ms, imageToConvert.RawFormat); byte[] RetVal = ms.ToArray(); ms.Close(); return ms.ToArray(); } }
public static Image ConvertByteArrayToImage(Byte[] Data) { using (MemoryStream ms = new MemoryStream()) { ms.Write(Data, 0, Data.Length); Bitmap Bmp = new Bitmap(ms); Bitmap Bmp2 = new Bitmap(Bmp.Width, Bmp.Height); Graphics gp = Graphics.FromImage(Bmp2); gp.DrawImage(Bmp, 0, 0, Bmp.Width, Bmp.Height); gp.Dispose(); Bmp.Dispose(); ms.Close(); return Bmp2; } }
That is better and saver method to convert Image to Byte Array and Byte Array to Image. Conversion method in your code depend on opened stream, and cause an error if the stream you are using is closed by garbage collector.
I am sorry about my bad english.
|
| Sign In·View Thread·PermaLink | 2.46/5 (6 votes) |
|
|
|
 |
|
|
I am getting the same error with your method when trying to convert a byte array to an image. The Bitmap Bmp = new Bitmap(ms) throws an error: 'Parameter is not valid.' Any thoughts on why?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Is accessible from the BitmapData class provided in the .NET API. Using Scan0 you can even get a pointer to the array. You can get the BitmapData instance by calling LockBits on a bitmap object.
On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. - Charles Babbage
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I'm guessing you did this so you could either send or compress the image data without directly implicating the file. If it were for sending, you may wish to consider simply creating a method that works for ALL OBJECTS by serializing the object to a memory stream and deserializing it when you wish to recreate the object. Doing so will give you greater flexibility and allow less hassle for converting other objects to byte arrays.
|
| Sign In·View Thread·PermaLink | 4.00/5 (2 votes) |
|
|
|
 |
|
|
I think that is better, in order to maximize performance, to code such a method with unmanaged code. I wrote a Class, based on a MSDN library article, that performs some transformations on a JPEG image with unmanaged code. the source code can be dowloaded from http://www.inf.ufrgs.br/~rrferreira/inf01046/SourceCodeTwo.zip. Just take a look inside the JPEG class that I coded.
cheers.
Ronaldo R Ferreira Porto Alegre, Brazil
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Not just that - it's a little misleading isn't it? You aren't really turning the image into an array of bytes in its current incarnation. You're saving the file to a stream, which isn't really much of an accomplishment. Additionally, you're converting or re-compressing the image in the process, which isn't necessarily intended or desired given the title of the post. Why not just title this - "How to save an image to a stream by using the save method", then I'd know not to even bother opening it.
|
| Sign In·View Thread·PermaLink | 4.43/5 (4 votes) |
|
|
|
 |