簡體   English   中英

在C#中繪制圖像

[英]Drawing on a Image in C#

我正在嘗試將圖像從剪貼板復制到現有圖像上。 基本上,現有圖像是150 X 150白色.jpg圖像。 (充當畫布)

我想知道如何從剪貼板上繪制我的圖像。

Image imgNew = Clipboard.GetImage(); //Getting the image in clipboard
Bitmap btnImg = new Bitmap(imgNew, 150, 100);  
Graphics g = Graphics.FromImage((Image)btnImg);
g.DrawImage(btnImg, 0, 0, 150, 100);

在這種方法中,它不會在已經存在的圖像上繪制。 實際上,我在這里使用Imagebox。 因此,將畫布設置為圖像框的圖像。

謝謝

您需要將圖像分配到圖片框

pictureBox1.Image = btnImg;

您應該使用using來確保在不再需要時釋放分配的資源。 完整代碼:

using (Image imgNew = Clipboard.GetImage()) //Getting the image in clipboard
{
    if (imgNew != null)
    {
        Bitmap btnImg = new Bitmap(imgNew, 150, 100);
        using (Graphics g = Graphics.FromImage((Image)btnImg))
            g.DrawImage(btnImg, 0, 0, 150, 100);
        pictureBox1.Image = btnImg;
    }
}

嘗試

Image imgNew = Clipboard.GetImage(); //Getting the image in clipboard
Graphics g = pictureBox1.CreateGraphics();
g.DrawImage(imgNew, 0, 0, 150, 100);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM