Anyone know how to convert back and forth between a System.Drawing.Bitmap and a byte[]?
Printable View
Anyone know how to convert back and forth between a System.Drawing.Bitmap and a byte[]?
hmmmmm
what do you mean by that? you want the color values in a bitmap? or what
I want the actual bytes of the file but I don't want to write a file
have you tried the MemoryStream class ( in System.IO ) , eg:
PHP Code:Bitmap bmp=new Bitmap(300 , 300);
Graphics grp = Graphics.FromImage(bmp);
Pen pn = new Pen(new SolidBrush(Color.BlueViolet),(float)5);
grp.DrawRectangle( pn , 10 , 10 , 100 , 100 );
grp.DrawString("test 123" , this.Font , new SolidBrush(Color.Red),new RectangleF(12 , 12 , 200,200));
System.IO.MemoryStream ByteStream = new System.IO.MemoryStream();
bmp.Save(ByteStream , System.Drawing.Imaging.ImageFormat.Bmp);
byte[] bytes = ByteStream.ToArray(); // put the Bitmap into a byte array.
ByteStream.Close();
bmp.Dispose();
So I can just Save to a stream that way eh..? cool.
hmm very interesting... could you tell me what use do you have for that though?
I have picture that are uploaded to a website. The binary info is then put in SQLServer. I need to be able to get a bitmap incase I want to edit it or something later. Plus, my code runs a web optimize on the graphics an converts them all to jpeg. This can be done at any time by admin. Full resize and so on to save space in SQL Server. Well, it did before my resize calculation blew up today. I hate 0.