方法 : LockBits を使用します。

[このドキュメントはプレビュー版であり、後のリリースで変更されることがあります。 空白のトピックは、プレースホルダーとして挿入されています。]

.NET Compact Framework LockBits メソッドをアンマネージ メモリ バッファー内のピクセルのビットマップの配列を操作し、ビットマップのピクセル、バッファーからピクセルに置き換えることができますサポートします。

.NET Compact Framework を受け取るパラメーターとして、 LockBitsBitmapData オーバーロード サポートしません。 ReadWriteは、 ReadOnlyWriteOnly 、および ImageLockMode 値を使用することができます。 .NET Compact Framework は以下の PixelFormat 値のみをサポートします。

.NET Compact Framework の PixelFormatImageプロパティをサポートしていない、ため formatLockBits パラメーターを使用してにそのピクセル形式の値を明示的に指定する必要があります注意ください。

使用例

ビットマップを作成し、LockBits を使用して一連の青のピクセルの輝度を変更する例を次に示します。

                        'Call method from OnPaint.
                        Protected
                        Overrides
                        Sub OnPaint(e As PaintEventArgs)
    LockUnlockBits(e)
EndSubPrivateSub LockUnlockBits(e As PaintEventArgs)
    Dim bmp AsNew Bitmap(".\test.bmp")

    ' Specify a pixel format.Dim pxf As PixelFormat = Pixelformat.Format24bppRgb

    ' Lock the bitmap's bits.Dim rect As Rectangle = New Rectangle(0,0,bmp.Width,Bmp.Height)
    Dim bmpData As BitmapData = bmp.LockBits(rect, _
        ImageLockMOde.ReadWrite, pxf)

    'Get the address of the first line of the bitmap.Dim ptr As IntPtr = bmpData.Scan0

    ' Declare an array to hold the bytes of the bitmap.    ' Dim numBytes as Integer = bmp.Width * bmp.Height * 3Dim numBytes as Integer = bmpData.Stride * bmp.Height
    Dim rgbValues(numBytes) AsByte
    ' Copy the RGB values into the array.
    Marshal.Copy(ptr, rgbValues, 0, numBytes)

    ' Manipulate the bitmap, such as changing the    ' blue value for every other pixel in the the bitmap.For counter AsInteger = 0 To rgbValues.Length Step 6
        rgbValues(counter) = 255
     Next counter

     ' Copy the RGB values back to the bitmap
      Marshal.Copy(rgbValues, 0, ptr, numBytes)

     ' Unlock the bits.
     bmp.UnlockBits(bmpData)


     ' Draw the modified image.
     e.Graphics.DrawImage(bmp, 0, 0)

     e.Graphics.Dispose()
EndSub
                        private Bitmap CreateBitmap(int width, int height, string s)
{
    Bitmap bmp = new Bitmap(width, height);

    Graphics g = Graphics.FromImage(bmp);
    g.FillRectangle(new SolidBrush(Color.LightCoral), 0, 0, bmp.Width, bmp.Height);
    g.DrawRectangle(new Pen(Color.Green, 10), 5, 5, bmp.Width - 10, bmp.Height - 10);
    g.DrawLine(new Pen(Color.Yellow, 15), 0, 0, bmp.Width, bmp.Height);
    g.DrawLine(new Pen(Color.Yellow, 15), bmp.Width, 0, 0, bmp.Height);

    SizeF size = g.MeasureString(s, this.Font);
    g.DrawString(s, this.Font, new SolidBrush(Color.Black),
                 (bmp.Width - size.Width) / 2,
                 (bmp.Height - size.Height) / 2);
    g.Dispose();

    return bmp;
}


protectedoverridevoid OnPaint(PaintEventArgs e)
{
    Bitmap bmp = CreateBitmap(100, 100, "Hi Mom!");
    e.Graphics.DrawImage(bmp, 0, 0);

    MakeMoreBlue(bmp);

    // Draw the modified image to the right of the original one
    e.Graphics.DrawImage(bmp, 120, 0);

    bmp.Dispose();
}


privatevoid MakeMoreBlue(Bitmap bmp)
{
    // Specify a pixel format.
    PixelFormat pxf = PixelFormat.Format24bppRgb;

    // Lock the bitmap's bits.
    Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
    BitmapData bmpData =
    bmp.LockBits(rect, ImageLockMode.ReadWrite,
                 pxf);

    // Get the address of the first line.
    IntPtr ptr = bmpData.Scan0;

    // Declare an array to hold the bytes of the bitmap.// int numBytes = bmp.Width * bmp.Height * 3;int numBytes = bmpData.Stride * bmp.Height;
    byte[] rgbValues = newbyte[numBytes];

    // Copy the RGB values into the array.
    Marshal.Copy(ptr, rgbValues, 0, numBytes);

    // Manipulate the bitmap, such as changing the// blue value for every other pixel in the the bitmap.for(int counter = 0; counter < rgbValues.Length; counter+=6)
        rgbValues[counter] = 255;

    // Copy the RGB values back to the bitmap
    Marshal.Copy(rgbValues, 0, ptr, numBytes);

    // Unlock the bits.
    bmp.UnlockBits(bmpData);
}

コードのコンパイル方法

この例では、次の名前空間への参照が必要です。

参照

処理手順

方法 : Off-Screen イメージを描画します。

その他の技術情報

グラフィックスと、.NET での描画の最適化フレームワーク