简体   繁体   English

如何将形状添加到现有的Picturebox图像C#

[英]How to add shape to an existent Picturebox image C#

I have created an Image and I want to add a rectangle around the image and recreate it as Image again to be drawn in a PictureBox but I'm getting out of memory exception 我已经创建了一个Image,我想在该图像周围添加一个矩形,然后将其重新创建为Image以便再次在PictureBox进行绘制,但是我的内存不足了

How can I modify the image. 如何修改图像。

public void Draw(SizeF size)
    {
        int scale = 100;
        Graphics refGraph = this.CreateGraphics();
        IntPtr hdc = refGraph.GetHdc();


        SolidBrush brush = new SolidBrush(Color.Black);
        Pen pen = new Pen(Color.Black, 4);
        try
        {
            Metafile image = new Metafile(hdc, EmfType.EmfOnly, "Shapes");
            using (Graphics g = Graphics.FromImage(image))
            {
                PointF center = new PointF((float)base.Width / 2, base.Height / 2);

                //Draw a rect
                RectangleF Block = new RectangleF(new PointF(center.X - size.Width * scale / 2, center.Y - size.Height * scale / 2), new SizeF(size.Width * scale, size.Height * scale));
                g.FillRectangle(brush, Block);
          }
            //Image = image;
            ModifyImage(image);
        }
        finally
        {
            refGraph.ReleaseHdc(hdc);
            refGraph.Dispose();
            pen.Dispose();
            brush.Dispose();
        }

        Invalidate();
    }

    public void ModifyImage(Metafile image)
    {
        Graphics g = Graphics.FromImage(image);
            PointF center = new PointF((float)Image.Width / 2, Image.Height / 2);
            int bufferAmount = 5;
            g.DrawRectangle(Pens.White, center.X - (Image.Width + bufferAmount) / 2, center.Y - (Image.Height + bufferAmount) / 2, Image.Width + bufferAmount, Image.Height + bufferAmount);

        pictureBox.Image = image;
    }

Thanks 谢谢

You can create a Graphics from the image using the FromImage method, then use Graphics drawing methods to draw whatever you like. 您可以使用FromImage方法从图像创建Graphics ,然后使用Graphics绘制方法绘制所需的Graphics Here is a code sample: 这是一个代码示例:

System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(yourImage);

//you may use any pen
graphics.DrawRectangle(System.Drawing.Pens.Blue,0,0,yourImageWidth,yourImageHeight)

yourPictureBox.Image = yourImage;

Create a Graphics object from your image and draw onto it by using a Pen defining the border strength and its color: 从图像中创建一个Graphics对象,并使用Pen定义边界强度及其颜色,在其上进行绘制:

using (var gfx = Graphics.FromImage(img))
{
    using (var pen = new Pen(MYCOLOR, 3)
        gfx.DrawRectangle(pen, MYRECT)
}

Note, this will manipulate your source image directly. 请注意,这将直接处理您的源图像。 If you want to have two images, one with and another without a border, you should clone your image before you draw over it: 如果要有两张图像,一张带有边框,另一张没有边框,则应绘制图像之前先对其进行克隆:

var imgWithBorder = img.Clone();
// work with imgWithBorder ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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