简体   繁体   中英

How to convert Graphic was drawn to byte[] array in C# (byte[] will also include drawings)

I want convert Graphic was drawn to byte[] array. This code will show everybody my ideas.

        Graphics newGraphics = Graphics.FromImage(image);

            // Draw it
            Random rnd = new Random();
            Color randomColor = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256));
            Pen blackPen = new Pen(randomColor, 2);

            // Create rectangle.               
            Rectangle rect = new Rectangle(int.Parse(val[2]), int.Parse(val[3]), int.Parse(val[4]), int.Parse(val[5])); //val[x] is 4 point to make Rectangle

            // Draw rectangle.
            newGraphics.DrawRectangle(blackPen, rect);
            newGraphics.DrawString(lines[int.Parse(val[0])], new Font("font name", 4), Brushes.Red, rect); //draw string for name of Rectangle

As you can see, after perform draw newGraphics is an image that has been drawn.

My idea got here, i don't know how to convert it to byte[] Because i still don't understand System.Drawing.Graphics of C#

Hope everybody help me. Thanks

How to convert Graphic was drawn to byte[] array in C# (byte[] will also include drawings)

When you use

Graphics newGraphics = Graphics.FromImage(image)

what you're basically doing is taking that original image and drawing directly to it using the Graphics methods.

When you're done drawing with Graphics you could just save the image or convert the image to a byte[].

Make sure to also Dispose() of the Graphics when your done as well, since it uses resources like memory and other [ IDisposable][4] objects that need to be cleaned up before they can be collected by the Garbage Collector

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM