在与笔的盘区的背景图象顶部画长方形 [英] draw rectangle over top of background image on panel with pen

查看:56
本文介绍了在与笔的盘区的背景图象顶部画长方形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨:

我已经开发了代码来在背景面板上扫描图像的不同部分,在将Otsu阈值处理应用到图像之后,并添加像素值。在此之后,它应评估值是高于还是低于设定的阈值。如果这些部分中的任何部分高于阈值,则应在部分周围绘制一个矩形。



下面的代码显示了数组填充和扫描以及绘制的不良尝试在图像上。

位图myImage; 
myImage =(Bitmap)Image.FromFile(openFileDialog1.FileName);
panel1.BackgroundImage = myImage;

// 用于保持每行像素总和的数组
float [] resultArray = new float [myImage.Height];

// 使用亮度值<填充来自每行像素的数据/ span>
for int i = 0 ; i > 26 && i < 39 ; i ++)
{

float value1 = 0 ;
for int j = 0 ; j > 22 && j < 35 ; j ++)
{
value1 + = myImage.GetPixel(j,i).GetBrightness();
}
resultArray [i] = value1;

if (value1 > 0
{

Pen pen = new Pen(Color.Red, 2 );
Graphics g = panel1.CreateGraphics();
g.DrawRectangle(笔, 10 10 50 50 );
}





这只是第一部分的一个数组。我想最好的办法是为绘图部分创建一个函数,并从交换机,case1,case2等传递数组的值。但我主要关心的是获取帮助来绘制上面详细的部分。我已将if条件设置为值> 0现在只是为了确定并测试它的工作原理。

谢谢。

解决方案

是的,如果你真的需要访问单个像素,唯一合理的方法就是使用 System.Drawing.Bitmap.LockBits

http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.lockbits.aspx [ ^ ]。



现在,如果你使用它的构造函数和绘图在某处创建 Graphics 的实例,图形将不会在屏幕上持久存在。您需要覆盖 System.Windows.Graphics.Control.OnPaint 或处理事件 System.Windows.Graphics.Control.Paint 并使用在事件参数中传递给处理程序的 Graphics 实例在处理程序中执行所有渲染。请查看我过去的答案:

什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ](这里解释了背景),

mdi子窗体之间的绘制线 [ ^ ],

在面板上捕获图纸 [ ^ ],

如何加快我的vb.net应用程序? [ ^ ]。



-SA

Hi:
I have developed code to scan different sections of an image on background panel, after Otsu Thresholding has been applied to the image, and add up pixel values. After this it should evaluate whether the values are above or below a set threshold value. If any of those sections are above the threshold value a rectangle should be drawn around the sections.

The code below shows the array population and scan and a poor attempt at drawing on the image.

Bitmap myImage;
myImage = (Bitmap)Image.FromFile(openFileDialog1.FileName);
panel1.BackgroundImage = myImage;

// Array for keeping the sums of each row of pixels
float[] resultArray = new float[myImage.Height];

// Populate the array with data from each row of pixels, using the brightness value
for (int i = 0; i > 26 && i < 39; i++)
{

    float value1 = 0;
    for (int j = 0; j > 22 && j < 35; j++)
    {
        value1 += myImage.GetPixel(j, i).GetBrightness();
    }
    resultArray[i] = value1;

    if (value1 > 0)
    {

        Pen pen = new Pen(Color.Red, 2);
        Graphics g = panel1.CreateGraphics();
        g.DrawRectangle(pen, 10, 10, 50, 50);
    }



This is just one array for the first section. I guess the best thing to do would be to create a function for the drawing part and just pass it the values of the arrays from a switch, case1, case2, etc. But my main concern is getting help to draw around the section detailed above. I have set the if condition to value > 0 for now just to be sure and test it works.
Thank you.

解决方案

Yes, if you really need to access individual pixels, the only reasonable way to go is using System.Drawing.Bitmap.LockBits:
http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.lockbits.aspx[^].

Now, if you are creating the instance of Graphics somewhere using its constructor and draw, the graphics will not persist on screen. You need to override System.Windows.Graphics.Control.OnPaint or handle the event System.Windows.Graphics.Control.Paint and do all your rendering in the handler, using the instance of Graphics passed to the handler in the event arguments. Please see my past answers:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^] (the background is explained here),
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^],
How to speed up my vb.net application?[^].

—SA


这篇关于在与笔的盘区的背景图象顶部画长方形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆