Converting an array of Pixels to an image in C#

前端 未结 6 1530
孤城傲影
孤城傲影 2020-12-28 11:20

I have an array of int pixels in my C# program and I want to convert it into an image. The problem is I am converting Java source code for a program into equiva

6条回答
  •  自闭症患者
    2020-12-28 11:53

    Using WPF, you can create a bitmap (image) directly from your array. You can then encode this image or display it or play with it:

    int width = 200;
    int height = 200;
    
    //
    // Here is the pixel format of your data, set it to the proper value for your data
    //
    PixelFormat pf = PixelFormats.Bgr32;
    int rawStride = (width * pf.BitsPerPixel + 7) / 8;
    
    //
    // Here is your raw data
    //
    int[] rawImage = new int[rawStride * height / 4];
    
    
    //
    // Create the BitmapSource
    //
    BitmapSource bitmap = BitmapSource.Create(
        width, height,
        96, 96, pf, null,
        rawImage, rawStride);
    

提交回复
热议问题