简体   繁体   中英

How To Use Int Array To Color.fromargb And Create A New Image c#

I have this code:

Image IG = new Bitmap(width,height);
Graphics myGraphic = Graphics.FromImage(IG);
Color c = Color.FromArgb(BData[i]);// int[] BData

I want to draw a new image and save it with that color - how can I do that?

    int[] BData = new int[4] { 255, 11, 255, 0 };
    int width = 640;
    int height = 480;
    using (System.Drawing.Image IG = new System.Drawing.Bitmap(width, height))
    {
        using (System.Drawing.Graphics myGraphic = System.Drawing.Graphics.FromImage(IG))
        {
            System.Drawing.Color c = System.Drawing.Color.FromArgb((byte)BData[0], (byte)BData[1], (byte)BData[2], (byte)BData[3]); // int[] BData
            myGraphic.Clear(c);
        }
        IG.Save("test.png", System.Drawing.Imaging.ImageFormat.Png);
    }

Using 256 for the alpha is going to insert a '0' alpha, the range is 0-255. This code saves a green .png image where the executable is. The Save() requires an extension and/or ImageFormat. You probably have to use i+0, i+1, i+2, i+3 in the Color.FromArgb. (byte)BData[i].

System.Drawing.Color.FromArgb((byte)BData[i+0], (byte)BData[i+1], (byte)BData[i+2], (byte)BData[i+3]);

Once the BData is filled, then just use:

using (var ms = new System.IO.MemoryStream(BData))
{
    System.Drawing.Image.Image image = System.Drawing.Image.FromStream(ms);
    image.Save(@"test.png");
}

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