1

I face an issue. I am trying to convert the Bitmap image into a memoryStream and store it. When converting the memoryStream back to image the background is becoming black

ms = new MemoryStream();
frm.pictureBox1.Image = Image.FromStream(ms);

as suggested above i tried Graphics.Clear() method to set the background Transparent. But no success. Still the background is black. Anyone please suggest a solution for this.

TaW
  • 53,122
  • 8
  • 69
  • 111
  • Possible duplicate of [C# Picturebox transparent background doesn't seem to work](https://stackoverflow.com/questions/5522337/c-sharp-picturebox-transparent-background-doesnt-seem-to-work) – Cee McSharpface Mar 21 '18 at 07:57
  • What format does the Image have? What is the source? Does it contain Transparency? Also: There is not enough code shown! – TaW Mar 21 '18 at 08:11
  • From what we see(!) this doesn't look like a duplicate.. – TaW Mar 21 '18 at 08:17
  • This should work: `MemoryStream ms = new MemoryStream(); pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat); pictureBox2.Image = Image.FromStream(ms); ms.Dispose();` – TaW Mar 21 '18 at 08:31
  • 1
    @TaW Actually, [closing the stream is against specs](https://msdn.microsoft.com/en-us/library/z7ha67kw(v=vs.110).aspx#Anchor_2) and is known to cause issues. These can be avoided by creating a new Bitmap object from the one created from stream (using the `new Bitmap(bmp)` constructor), and then disposing both the image-from-stream and the stream while keeping the object created from the `new Bitmap(bmp)` constructor. (It has the slight disadvantage of converting the image to 32bpp, but if it's just for showing on UI that should be no problem) – Nyerguds Mar 21 '18 at 23:30
  • 1
    On the question... you should show more code. What you show is making an image from a new, empty `MemoryStream`. That can never work. The stream should at least be initialized using a byte array. – Nyerguds Mar 21 '18 at 23:32

0 Answers0