Image upload to PictureBox

0
0

Hello again ūüôā

So I’m uploading image files and assigning them to a PictureBox, thusly:

private void UploadButton_Uploaded(object sender, UploadedEventArgs e)
{
System.Drawing.Image I = System.Drawing.Image.FromStream(e.Files[0].InputStream);
PictureBox PB = new PictureBox();

PB.SizeMode = PictureBoxSizeMode.Zoom;
PB.Image = I;
PB.Movable = true;
PB.Location = new Point(0, 0);
MainPanel.Controls.Add(PB);
}

This works for a good number of images. Sometimes, however, I get what appears to be a valid Image¬†object (when I breakpoint and inspect the Image variable)¬†after the upload, but the PictureBox doesn’t draw the image.

Is there something I need to do to force the image to transfer to the client?

By the way,¬†it doesn’t appear to be an issue of size, some of the ones that work are larger than some that don’t.

Also…these were all uploading properly when I was using the previous GDI paint method, so I’m sure the Image is good.

Thanks!

-John

  • You must to post comments
0
0

System.Drawing.Image.FromStream requires that the stream stays open for the entire lifetime of the image object, but the file streams returned in System.Web.HttpFileCollection are closed when the request is completed. Basically it loads the image progressively. When you debug or paint the image it forces the System.Drawing library to load the stream.

You can use new Bitmap(Image.FromStream()), or copy the stream into a memory stream. If you use new Bitmap(Image.FromStream()) and you upload an animate gif it will become just an image. For animated gifs it needs a stream. Look at the UploadFiles example. The code is also on github, see Window1.cs.

 

  • John Nagle
    Ahhhhhhhh….That makes perfect sense…thank you Luca for taking time on a Sunday to answer this question! -John
  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.