Set Pixels to Bitmap - CSharp System.Drawing

CSharp examples for System.Drawing:Image Operation

Description

Set Pixels to Bitmap

Demo Code


using System.Drawing.Imaging;
using System.Drawing;
using System.Collections;
using System;/*from   ww w.  j  a va2 s.  c  om*/

public class Main{
    public static unsafe void SetPixels(Bitmap bmp, int[,] pixels)
      {
         BitmapData bmpData=bmp.LockBits(new Rectangle(0,0,bmp.Width,bmp.Height),
            ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
         System.IntPtr Scan0 = bmpData.Scan0;
         byte* scan0=(byte*)(void*)Scan0;
         int* ptr = (int*)scan0;

         int strideDiff = bmpData.Stride - bmp.Width*4; // TODO: bytesPerPixel

         for (int y = 0; y < bmp.Height; y++)
         {
            for (int x = 0; x < bmp.Width; x++)
            {
               *ptr = pixels[x,y];
               ptr++;
            }
            ptr+=strideDiff;
         }
         bmp.UnlockBits(bmpData);
      }
}

Related Tutorials