1

I render a png on a empty custom User Control like this:

 e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
 e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
 var drawRect = new Rectangle(0, 0, (int)(imgWidth* ImageScale), (int)(imgHight* ImageScale));
 e.Graphics.DrawImageUnscaledAndClipped(myImage, drawRect);

So that I can see properly the pixels without antialiasing and scale the image.

Through the MouseMove event I save the mouse position on a property of the control and then I show the pixel on which the mouse is currently on, like this:

e.Graphics.FillRectangle((Brush)Brushes.Black, _mousePosition.X, _mousePosition.Y, (int)ImageScale, (int)ImageScale);

I have two problem:

  1. the pixel is of the right size, but its position (when rendered) is continuous and do not follow the pixel grid as I wanted
  2. I had to insert Invalidate() at the end of the MouseMove event to force the pixel rendering, maybe there is an better way to do this
Vasari
  • 13
  • 2
  • 1) It looks like you're not scaling the Mouse position, which is relative to the Control's surface, not the scaled Image 2) If you draw the black rectangle on the Control's surface using its `Paint` event / `OnPaint()` override, you need to call `Invalidate()` to redraw it at that time. That method has overloads that allows to specify the section of the drawing to repaint. I assume the UC is double-buffered – Jimi Aug 05 '22 at 12:15
  • Can you elaborate on the first point? Thanks for the second point: I had to store the previous mouse position to ensure that the "old pixel" was deleted before showing the current. – Vasari Aug 05 '22 at 12:35
  • Assume the image you're drawing [looks like this](https://stackoverflow.com/a/54726707/7444103). Consider the green box at the upper left corner (it's a scaled pixel). When the Mouse position is at `(0,0)`, you draw a Rectangle that includes that Pixel alone, using the scale factor (`ImageScale`) to size the rect. Now, if the Mouse moves at position `(1, 1)`, the actual position should still be `(0, 0)`, since the `X` and `Y` values are still below the value represented by `ImageScale`. I.e., you have to *normalize* the Mouse position to the scale factor (it's just a division). – Jimi Aug 05 '22 at 13:20
  • If I divide the e.X or e.Y of the mousemove event by the scalefactor the movement is still smooth as before but the pixel is drawned far from the mouse actual position. – Vasari Aug 05 '22 at 13:49
  • Don't you want to draw a black rectangle over the current scaled Pixel? That's what the code you seems to be doing. If that's not the intended behavior, then add to the question an extended description of how the current result is different from what is expected and explain what is actually expected. A couple of images / animations could help. -- Also, give context to the code you have posted, don't remove the method(s) that contain(s) the code. – Jimi Aug 05 '22 at 14:03
  • A double convertion was needed. first I had to divide the coordinates by the scale factor, then at last on drawing I had to multiply the normlized coordinates by the scale factor again – Vasari Aug 05 '22 at 17:40

0 Answers0