Asking Questions
Asking Questions

Reputation: 404

Bitmap not seeing graphics

I have been looking for about a week now for an answer but all in vain I have code here

    Dim bmap As Bitmap

    bmap = New Bitmap(PictureBox1.Width, PictureBox1.Height)

    Dim g As Graphics = graphics.FromImage(bmap)

    g.FillRectangle(Brushes.Black, 0, 0, 100, 100)

    For q As Integer = 0 To bmap.Width - 1

        For w As Integer = 0 To bmap.Height - 1

            If bmap.GetPixel(q, w) = Color.Black Then

                bmap.SetPixel(q, w, Color.Green)

            End If
        Next

    Next

   PictureBox1.Image = bmap

so when I click the button it will draw the 100 by 100 black box but it will not set the pixels to green

so the bitmap is not recognizing the graphics

Upvotes: 1

Views: 59

Answers (1)

qwr
qwr

Reputation: 3670

From msdn about Color equality operator:

This method compares more than the ARGB values of the Color structures. It also does a comparison of some state flags. If you want to compare just the ARGB values of two Color structures, compare them using the ToArgb method.

So to compare one must use ToArgb method

 If bmap.GetPixel(q, w).ToArgb = Color.Black.ToArgb Then

Some internals from source codes. We see that For Color.Black which is KnownColor this ctor will be called

   internal Color(KnownColor knownColor) {
            value = 0;
            state = StateKnownColorValid;
            name = null;
            this.knownColor = (short)knownColor;
        }

but for GetPixel Color.FromArgb(value) is called

private Color(long value, short state, string name, KnownColor knownColor) {
            this.value = value;
            this.state = state;
            this.name = name;
            this.knownColor = (short)knownColor;
        }

 public static Color FromArgb(int argb) {
            return new Color((long)argb & 0xffffffff, StateARGBValueValid, null, (KnownColor)0);
        }

So another fix for your case can be

If bmap.GetPixel(q, w) = Color.FromArgb(&HFF000000) Then

Upvotes: 1

Related Questions