繁体   English   中英

如何在Graphics.DrawImage中扩展绘图区域c#

[英]how to extend draw area in Graphics.DrawImage c#

我有一个Rectangle(rec),其中包含在较大图像中包含较小图像的区域。 我想在图片框上显示此较小的图像。 但是,我真正在做的是将较小的图像用作333x324较大图像的图片检测器。 所以我想做的是使用较小图像矩形的坐标,然后从矩形的左侧开始绘制到“图片框”,然后以333宽度和324高度向外移动。

目前,我的代码可以运行,但是只显示用于检测目的的小图像。 我希望它显示较小的图像+ 300宽度和+ 300高度。

我花了几个小时弄弄这段代码,我必须做一些极其基本的错误。 如果有人可以帮助我,我将非常感谢!

我的课程代码:

public static class Worker
{

    public static void doWork(object myForm)
    {
        //infinitely search for maps
        for (;;)
        {


            //match type signature for Threading
            var myForm1 = (Form1)myForm;

            //capture screen
            Bitmap currentBitmap = new Bitmap(CaptureScreen.capture());

            //detect map
            Detector detector = new Detector();
            Rectangle rec = detector.searchBitmap(currentBitmap, 0.1);

            //if it actually found something
            if(rec.Width != 0)
            {
                // Create the new bitmap and associated graphics object
                Bitmap bmp = new Bitmap(rec.X, rec.Y);
                Graphics g = Graphics.FromImage(bmp);

                // Draw the specified section of the source bitmap to the new one
                g.DrawImage(currentBitmap, 0,0, rec, GraphicsUnit.Pixel);

                // send to the picture box &refresh;
                myForm1.Invoke(new Action(() =>
                {
                    myForm1.getPicturebox().Image = bmp;
                    myForm1.getPicturebox().Refresh();
                    myForm1.Update();
                }));

                // Clean up
                g.Dispose();
                bmp.Dispose();

            }

            //kill
            currentBitmap.Dispose();

            //do 10 times per second
            System.Threading.Thread.Sleep(100); 

        }
    }
}

如果我理解正确,则rec变量包含具有正确XY的矩形,该矩形标识出Width=333Height=324的矩形。

因此,在if语句中,首先设置所需的大小:

rec.Width = 333;
rec.Height = 324;

然后,请注意, Bitmap构造函数需要宽度和高度,因此请更改

Bitmap bmp = new Bitmap(rec.X, rec.Y);

Bitmap bmp = new Bitmap(rec.Width, rec.Height);

就是这样-代码的其余部分可以保持现在的样子。

暂无
暂无

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

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