Andrew
Andrew

Reputation: 552

Scale x and y coordinates to Form/pictureBox location. C#

My Form2 is in layman terms an external 'mini-map' off the actual 'mini-map' in-game.

As you can see on my Form2, my drawn red dot does not have the same location for my player when compared to the 'mini-map' in-game which is the yellow dot.

In the DebugView, you can see my characters X and Y location (charX & charY).

The coordinates are passed as int x & int y in a function to my Form2 class file.

The image in my pictureBox1 (which is the image in the current example picture above) is pulled from my server (url= "http://randomspam.co/MAP/103000000.img/miniMap.canvas.png").

Here is the following code with comments to my progress as of now.

Please take note that the pictureBox1 location is set to 0,0.

The errors are as follows;

1) The red dot location on my external mini-map != the location of my character in the mini-map in-game.

2) The red dot consistently flickers (appears & disappears)

3) The tooltip when shown on the pictureBox is really lagging in revealing and dis-revealing itself.

If anyone knows how to help out my current situation (as I am lost), please, anything is appreciated.

Thanks.

Upvotes: 1

Views: 2663

Answers (2)

Have a copy from the mini map:

Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Bitmap bmpClone = new Bitmap(pictureBox1.Width, pictureBox1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

Graphics objGraphics = Graphics.FromImage(bmpClone);
objGraphics.DrawImage(pictureBox1.Image, 0, 0);
objGraphics.Dispose();

bmp = (Bitmap)bmpClone.Clone();
pictureBox1.Image = bmp;

Now before any invalidation do:

Graphics objGraphics = Graphics.FromImage(bmp);
objGraphics.SmoothingMode = SmoothingMode.HighQuality;
objGraphics.DrawImage(bmpClone, 0, 0);
objGraphics.FillEllipse(Brushes.Red, cx, cy, 5, 5)
objGraphics.Dispose();

pictureBox1.Invalidate();

You dont need anything inside pictureBox1_Paint

valter

Upvotes: 1

tweellt
tweellt

Reputation: 2171

Ok, lets split this in topics:

1) Red Dot Location:

Here you have to match red dot position to the new size, this was answered several times before, see this -> How can I transform XY coordinates and height/width on a scaled image to an original sized image?

2) Double Buffer to stop Flickering:

public void DrawWhatever(Graphics graphics, int cx, int cy)
{
    Graphics g;
    Bitmap buffer = null;
    buffer = new Bitmap([image width], [image height], graphics);
    g = Graphics.FromImage(buffer);
    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

    // Draw a circle.
    Pen p = new Pen(Color.Red,1)
    g.DrawEllipse(p,cx,cy,30,30); //example values

    graphics.DrawImage(buffer, 0, 0);
    g.Dispose();
}

3) Tooltip:

Check double buffer algorithm and let me know

Upvotes: 2

Related Questions