简体   繁体   中英

Drawing on a Image in C#

I'm trying to copy an image from the clipboard onto an existing image. Basically the existing image is 150 X 150 white coloured .jpg image. (Acting as the canvas)

I would like to know how to draw my image from clipboard onto this....

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);

In this method it is not drawing on the already existing image. Actually I'm using an Imagebox here. So the canvas is set as the imagebox's image.

Thanks

You will need to assign the image to the picturebox

pictureBox1.Image = btnImg;

You should use using to make sure allocated resources are freed when no longer needed. The full code:

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;
    }
}

Try

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM