Note that there are some explanatory texts on larger screens.

plurals
  1. POSaving Image to MemoryStream- Generic GDI+ Error
    primarykey
    data
    text
    <p>Overview of my application: On the client side, a series of snapshots are taken with a webcam. On submit, I want the images to be converted to a byte array, and have that byte array sent to a service I have written.</p> <p>My problem: I'm trying to save a single image to a <code>MemoryStream</code>, but it continues to break, spitting out the message, <strong><em>"A generic error occured in GDI+."</em></strong> When I dig deeper, I see that the exception is thrown when the MemoryStream's buffer position is at 54. Unfortunately, it's a <code>1.2 mb</code> photo. Here's the block of code:</p> <pre><code>// Create array of MemoryStreams var imageStreams = new MemoryStream[SelectedImages.Count]; for (int i = 0; i &lt; this.SelectedImages.Count; i++) { System.Drawing.Image image = BitmapFromSource(this.SelectedImages[i]); imageStreams[i] = new MemoryStream(); image.Save(imageStreams[i], ImageFormat.Bmp); /* Error is thrown here! */ } // Combine MemoryStreams into a single byte array (Threw this // in in case somebody has a better approach) byte[] bytes = new byte[imageStreams.Sum(s =&gt; s.Length)]; for(int i = 0; i &lt; imageStreams.Length; i++) { bytes.Concat(imageStreams[i].ToArray()); } </code></pre> <p>And here is my BitmapFromSource method</p> <pre><code>// Converts a BitmapSource object to a Bitmap object private System.Drawing.Image BitmapFromSource(BitmapSource source) { System.Drawing.Image bitmap; using (MemoryStream ms = new MemoryStream()) { BitmapEncoder encoder = new BmpBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(source)); encoder.Save(ms); bitmap = new System.Drawing.Bitmap(ms); } return bitmap; } </code></pre> <p>A lot of what I have read about the Generic GDI+ Error points to a permissions issue, but I don't see how that would apply here, considering I'm not saving to the file system. Also, I've seen that this error can arise due to the MemoryStream closing before the image is being saved, but I also don't see how this would apply considering I create the MemoryStream immediately before I save the image. Any insight would be greatly appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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