c# 像素 转 图像 byte 转 图像 char 转 图像

1、
int  width = 50; 
int  height = 50; 
var  bitmap =  new  Bitmap(width, height, PixelFormat.Canonical);
 
for  ( int  y = 0; y < height; y++)
    for  ( int  x = 0; x < width; x++)
    {
       int  red = 0; 
       int  green = 0; 
       int  blue = 0; 
       bitmap.SetPixel(x, y, Color.FromArgb(0, red, green, blue));
    }
}

2、

//using System.Windows.Media.Imaging;
//using System.Windows.Media;
//using System.Drawing;
public static void savepng(WriteableBitmap wtbBmp, string filename)
        {
            if (wtbBmp == null)
            {
                return;
            }
            try
            {
                RenderTargetBitmap rtbitmap = new RenderTargetBitmap(wtbBmp.PixelWidth, wtbBmp.PixelHeight, wtbBmp.DpiX, wtbBmp.DpiY, PixelFormats.Default);
                DrawingVisual drawingVisual = new DrawingVisual();
                using (var dc = drawingVisual.RenderOpen())
                {
                    dc.DrawImage(wtbBmp, new Rect(0, 0, wtbBmp.Width, wtbBmp.Height));
                }
                rtbitmap.Render(drawingVisual);
                //System.Windows.Media.Imaging.
                //BmpBitmapEncoder bitmapEncoder = new BmpBitmapEncoder();
                PngBitmapEncoder bitmapEncoder = new PngBitmapEncoder();
                bitmapEncoder.Frames.Add(BitmapFrame.Create(rtbitmap));


                if (!File.Exists(filename))
                {
                    FileStream file = new FileStream(filename, FileMode.OpenOrCreate);
                    //bitmapEncoder.Save(File.OpenWrite(filename));


                    MemoryStream stream = new MemoryStream();
                    bitmapEncoder.Save(stream);
                    long size = stream.Seek(0, SeekOrigin.Current);
                    file.Write(stream.ToArray(), 0, (int)size);
                    stream.Close();
                    file.Close();
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
        }


        


        static void Main(string[] args)
        {
            int width = 600;
            int height = 600;
            WriteableBitmap tempBitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Bgra32, null);
            tempBitmap.Lock();




            int size = 600 * 600;
            byte[] color = new byte[4] { 100, 100, 100, 255 };//bgra
            byte[] content = new byte[size*4];        //第一字节不能为空,否则会保存图像失败
            for (int i = 0; i < size; i++)
            {
                content[i * 4 + 0] = color[0];
                content[i * 4 + 1] = color[1];
                content[i * 4 + 2] = color[2];
                content[i * 4 + 3] = color[3];
            }


            tempBitmap.WritePixels(new Int32Rect(0, 0, width, height), content, width * 4, 0);


            tempBitmap.Unlock();
            savepng(tempBitmap, "E:/aaa/test.png");
            //Bitmap bmp = new Bitmap(128, 128, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            //Console.Read();
        }

猜你喜欢

转载自blog.csdn.net/qiushangren/article/details/69384684