Note that there are some explanatory texts on larger screens.

plurals
  1. POC#: Getting Error: "A generic error occurred in GDI+" When trying to copy imagebox.Image into memory stream. This stream is not being saved to file.
    primarykey
    data
    text
    <p>I am getting an odd error with <code>Image.save(Stream, Format)</code>.</p> <p>I tried looking around on here for a solution but everyone seems to think the error is from file permissions. That can't be it in my case case the Stream isn't going into a file. My code is below:</p> <pre><code>MemoryStream Stream = new MemoryStream(); this.Image_Box_1.Image.Save(Stream, System.Drawing.Imaging.ImageFormat.Jpeg); TI.AlbumCover = Stream.ToArray(); Stream.Close(); </code></pre> <p><code>TI.AlbumCover</code> is a <code>byte[]</code>.</p> <p>Does anyone have any ideas on what the problem might be?</p> <p>EDIT:</p> <p>Ok, so I worked it out. The original file could sometimes come from a jpg file, sometimes from a byte array (part of an id3 tag). The problem was that when the image came from the file, I was closing the stream after creating the image box image. While the image remained visible, the data was no longer available.</p> <p>Since I also later needed to overwrite that jpg file, I could not simply leave the filestream for it open so I left the rest of my code the same and changed the code to read from the jpg to the following:</p> <pre><code>FileStream FS = new FileStream(File, FileMode.Open, FileAccess.Read);//Read in the jpg file Image IMG = Image.FromStream(FS);//Create an image from the file data MemoryStream MS = new MemoryStream(); IMG.Save(MS, System.Drawing.Imaging.ImageFormat.Jpeg);//Save the image data to a memory stream byte[] temp = MS.ToArray();//Copy the image data to a byte array //close the streams MS.Close(); FS.Close(); return temp; //was originally returning an image </code></pre> <p>Then after executing this code I change the code that placed the image into the image box to:</p> <pre><code>try { if (this.m_V2Tag.AlbumCover != null) this.Image_Box_1.Image = Image.FromStream(new MemoryStream(this.m_V2Tag.AlbumCover)); //changed code else { MemoryStream temp = new MemoryStream(this.getFolderJpg()); //create a memory stream from the byte[]. This stream can safely be left open. this.Image_Box_1.Image = Image.FromStream(temp); // create image and assign it to the image box } } catch { this.Image_Box_1.Image = null; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload