Click here to Skip to main content
15,895,011 members

draw rectangle over top of background image on panel with pen

defunktlemon asked:

Open original thread
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.
C#
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.
Tags: C#

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900