Share via


方法 : イメージをコピーします。

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

.NET Compact Framework では、Image.Clone 方法をサポートしていませんがまだコピー イメージとイメージの部分。 次の操作を行う例を次に示します。

  • ビットマップを作成するメソッドを定義します。

  • ビットマップをコピーする] または [ビットマップの一部をコピーするオーバーロードされたメソッドを定義します。

  • これらのメソッドし、フォームの OnPaint メソッドをオーバーライドして画面に、イメージを描画します。

ビットマップを作成するには

  • このメソッドはデモンストレーション用ビットマップを作成します。
                        ' Creates a bitmap for copying.
                        Function CreateBitmap(sideSize AsInteger) As Bitmap
    Dim bmp AsNew Bitmap(sideSize, sideSize)
    Dim g As Graphics = Graphics.FromImage(bmp)

    g.FillEllipse(New SolidBrush(Color.Red), 0, 0, sideSize, sideSize)
    g.DrawLine(New Pen(Color.Black), 0, 0, sideSize, sideSize)
    g.DrawLine(New Pen(Color.Black), sideSize, 0, 0, sideSize)
    g.Dispose()

    Return bmp
EndFunction
                        // Creates a bitmap for copying.
                        private Bitmap CreateBitmap(int sideSize)
{
    Bitmap bmp = new Bitmap(sideSize, sideSize);
    Graphics g = Graphics.FromImage(bmp);

    g.FillEllipse(new SolidBrush(Color.Red), 0, 0, sideSize, sideSize);
    g.DrawLine(new Pen(Color.Black), 0, 0, sideSize, sideSize);
    g.DrawLine(new Pen(Color.Black), sideSize, 0, 0, sideSize);
    g.Dispose();

    return bmp;
}

ビットマップを複製するには

  • このメソッド オーバーロードではパラメーターとしてソース ビットマップを受け取り、コピーとしてビットマップを返します。
                        ' Copies the entire bitmap.
                        Overloads
                        Function CopyBitmap(source As Bitmap) As Bitmap
    ReturnNew Bitmap(source)
EndFunction
                        // Copies the entire bitmap.
                        protected Bitmap CopyBitmap(Bitmap source)
{
    returnnew Bitmap(source);
}

ビットマップの一部をコピーするには

  • このメソッド オーバーロードは戻りますビットマップの一部の寸法を決定するパラメーターとして、Rectangle を受け取ります。
                        ' Copies a part of the bitmap.
                        Overloads
                        Function CopyBitmap(source As Bitmap, part As Rectangle) As Bitmap
    Dim bmp AsNew Bitmap(part.Width, part.Height)

    Dim g As Graphics = Graphics.FromImage(bmp)
    g.DrawImage(source, 0, 0, part, GraphicsUnit.Pixel)
    g.Dispose()

    Return bmp
EndFunction
                        // Copies a part of a bitmap.
                        protected Bitmap CopyBitmap(Bitmap source, Rectangle part)
{
    Bitmap bmp = new Bitmap(part.Width, part.Height);
    Graphics g = Graphics.FromImage(bmp);
    g.DrawImage(source,0,0,part,GraphicsUnit.Pixel);
    g.Dispose();
    return bmp;
}

作成、コピー、および、ビットマップを描画するには

  • OnPaint このメソッド オーバーロードは、ビットマップを作成し、複製し、部分をコピーする方法を呼び出します。 ファイルに、クローンとして作成されたビットマップを保存することも。
                        ' Draws the bitmaps on the form.   
                        Protected
                        Overrides
                        Sub OnPaint(e As PaintEventArgs)
    Dim arialFont As Font
    Dim blackBrush As Brush

    arialFont = New Font("Arial", 10, FontStyle.Regular)
    blackBrush = New SolidBrush(Color.Black)

    ' Set the size of the sides of the bitmap,    ' and get one-third of it for the center bitmap.Dim sidesize AsInteger = 75
    Dim third AsInteger = CInt(sidesize / 3)

    ' Create bitmap.Dim source As Bitmap = CreateBitmap(sidesize)

    ' Copy entirely as a clone.Dim clone As Bitmap = CopyBitmap(source)

    ' Copy the center part of the bitmap.Dim center As Bitmap = _ 
        CopyBitmap(source, New Rectangle(third, third, third, third))

    ' Save the bitmap to a file.
    clone.Save("newbitmap.bmp", ImageFormat.Bmp)

    ' Draw the source, clone, and partial     ' bitmaps vertically down the screen. Dim y AsInteger = 10

    e.Graphics.DrawString("source bitmap:", ArialFont, BlackBrush, 10, y)
    y += 20

    e.Graphics.DrawImage(source, 10, y)
    y += source.Height + 10

    e.Graphics.DrawString("clone bitmap:", ArialFont, BlackBrush, 10, y)
    y += 20

    e.Graphics.DrawImage(clone, 10, y)
    y += clone.Height + 10

    e.Graphics.DrawString("center part of bitmap:", ArialFont, BlackBrush, 10, y)
    y += 20

    e.Graphics.DrawImage(center, 10, y)
    y += center.Height + 10

    ' Dispose graphic objects.
    arialFont.Dispose()
    blackBrush.Dispose()

EndSub
                        // Draws the bitmaps on the form.   
                        protected
                        override
                        void OnPaint(PaintEventArgs e)
{
    Font arialFont;
    Brush blackBrush;
    arialFont = new Font("Arial", 10, FontStyle.Regular);
    blackBrush = new SolidBrush(Color.Black);

    // Set the size of the sides of the bitmap,// and get one-third of it for the center bitmap.int sidesize = 75;
    int third = (int) sidesize/3;

    // Create bitmap.
    source = CreateBitmap(sidesize);

    // Copy entirely as a clone.
    clone = CopyBitmap(source);

    // Copy the center part of the bitmap.
    center = CopyBitmap(source, new Rectangle(third, third, third, third));

    // Save the bitmap to a file.
    clone.Save("newbitmap.bmp", ImageFormat.Bmp);

    // Draw the source, clone, and partial // bitmaps vertically down the screen. int y = 10;

    e.Graphics.DrawString("source bitmap:", arialFont, blackBrush, 10, y);
    y += 20;

    e.Graphics.DrawImage(source, 10, y);
    y += source.Height + 10;

    e.Graphics.DrawString("clone bitmap:", arialFont, blackBrush, 10, y);
    y += 20;

    e.Graphics.DrawImage(clone, 10, y);
    y += clone.Height + 10;

    e.Graphics.DrawString("center part of bitmap:", arialFont, blackBrush, 10, y);
    y += 20;

    e.Graphics.DrawImage(center, 10, y);
    y += center.Height + 10;

    // Dispose graphic objects.
    arialFont.Dispose();
    blackBrush.Dispose();
}

コードのコンパイル方法

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

堅牢性の高いプログラム

Font Brush オブジェクトとは、 OnPaint メソッド オーバーロードで破棄明示的に確認します。 Graphics オブジェクトの Graphics プロパティによって返される PaintEventArgs オブジェクトはガベージ コレクターによって破棄されする明示的に破棄する必要はありません。

参照

その他の技術情報

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