1

my first question here. And I'm sure it's an easy one, but I can't find a solution anywhere, I've read lots and just can't comprehend how to what i need to do:

  • I have a *.gif file, that I want to store in System.Drawing.Bitmap, so that I have access to GetPixel() method
  • I want to have my drawing on some control (currently trying out PictureBox)

So far what I've got:

  • I create a template, Bitmap from file - and that works (map_t in code)
  • I create a bitmap that will actually be drawn (map)
  • I can set my PictureBox image to a bitmap
    • I can't change this bitmap. Or I can, but the result doesn't show
    • I thought I'd have to create Graphic from that bitmap, then change it. I have no idea what i'm missing.

Here's the code: MapCanvas is PictureBox on a form, everything is in MouseMove, because I was trying to make some circles around the pointer - didn't work so I ended up with this code, which doesn't work either. I have read every question I could find, nothing helps... Here's the code:

namespace Projekt_innowacje
{
    public partial class MapForm : Form
    {
        Bitmap map_t;
        Bitmap map;

        public MapForm()
        {
            InitializeComponent();
            map_t = new Bitmap("map.gif", true);
            map = new Bitmap(map_t.Width, map_t.Height);
            MapCanvas.Image = map;
        }

        private void MapCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            Graphics canvas = Graphics.FromImage(map);
            for (int i = 0; i <= map.Width; i++)
                for (int j = 0; j <= map.Height; j++)
                    if (map.GetPixel(i, j) == Color.Red)
                        canvas.DrawLine(new Pen(Color.Blue), i, j, i + 1, j + 1);

                        // map.SetPixel(i, j, map.GetPixel(i, j)); // also doesn't work         

            //MapCanvas.Refresh(); 
            MapCanvas.Invalidate();

            canvas.Dispose();
        }
    }
}
wasyl
  • 3,421
  • 3
  • 27
  • 36
  • Have you tried using a BMP or a JPEG? GIFs use indexed color and usually cannot be arbitrarily drawn to. – Ani Apr 30 '12 at 22:17
  • This is not the issue, as I only read pixel from GIF file. As you can see, I try to draw on new, blank bitmap of size of the gif file. – wasyl Apr 30 '12 at 22:22

1 Answers1

1

The code you are showing will never modify the canvas/map image unless you are making some other changes to the map image that you're not sharing. In your loop you test map.GetPixel, however map is created as a blank image in the constructor, so none of its pixels will ever be Color.Red. Did you intend to test against your template instead?

Also, you may want to have a look at this question which talks about comparing colors. It suggests that for basic color comparisons to use the .ToArgb() method on the color structure since the == and Equals do more than just compare the color value.

Community
  • 1
  • 1
heavyd
  • 17,303
  • 5
  • 56
  • 74
  • I am not making any other changes. You was right, I wanted to test against map_t, but even commenting this test doesn't help. So what am I lacking if I want to change `map` and then draw those changed image? Commented line `map.SetPixel(...)` instead of previous one doesn't help, and this is a mystery for me. Also, this code is quite similar to what I have found already, but no code was for the exact thing that I need, so I had to alter it - in a wrong way, apparently... – wasyl Apr 30 '12 at 23:14
  • I don't think your problem is updating the picture box, what you're doing should update the box (see simple example: https://gist.github.com/2563600). Your `if` statement is keeping you from ever changing the image. – heavyd Apr 30 '12 at 23:23
  • Thank You very much! The test was wrong, but what in fact caused my problem was that `MapForm.Enabled` was set to `False`! And because of that, MouseMove event wouldn't be called at all... You helped me find it by saying this should work - and it does (both ways, `map.SetPixel` and `canvas.DrawLine`. I probably will be doing this for some thime though. Thanks again! :) – wasyl Apr 30 '12 at 23:59