0

I'm writing a simple program in C# and Windows Forms for manipulating pixel art images and i need to scale these images as the resolution of the canvas changes.

I'm drawing the images on a PictureBox and using an Matrix to scale its graphics object to a certain resolution, and i need a method to scale these images correctly in all directions, based on whatever is the new resolution, without making their pixels become blurred, etc.

Here is the method i'm using to scale the graphics:

public void PictureBox_Paint(object sender, PaintEventArgs e)
{
    var graphics = e.Graphics;
    graphics.MultiplyTransform(ScaleMatrix());
}

public float VirtualWidth = 1024;
public float VirtualHeight = 768;

public Matrix ScaleMatrix()
{
    var matrix = new Matrix();

    var x = (float) this.PictureBox.Width / VirtualWidth;
    var y = (float) this.PictureBox.Height / VirtualHeight;

    matrix.Scale(x, y);

    return matrix;
}

What would be the correct implementation to achieve this task?

RickS.
  • 1
  • 1
  • [Zoom and translate an Image from the mouse location](https://stackoverflow.com/a/61964222/7444103) – Jimi Sep 15 '21 at 02:37
  • With *resolution* you probably mean just *Size*. If your app needs to work with different Monitors that have different resolution, size and scale, you need to consider the DPI resolution of the Image you're working with. Always create a copy of the original Image and use the `SetResolution(dpiX, dpiY)` method, so the new Bitmaps uses the same resolution as the current Monitor (see the `DeviceDpi` Property). -- To scale an Image, you just need to calculate the scale. Store the canvas initial size and the drawn Image size. When the canvas scales, apply the same scale to the Image bounds. – Jimi Sep 15 '21 at 02:44
  • 1
    See also the notes here: [Disable Image blending on a PictureBox](https://stackoverflow.com/a/54726707/7444103) and here: [Resizing a Bitmap used as watermark the result shows dark borders](https://stackoverflow.com/a/61977870/7444103) – Jimi Sep 15 '21 at 02:57
  • Thanks for the comment. The "Disable image blending" link you posted seems to solve my problem but this is in VB and i'm having some difficulty to understand it, so coult you write and answer here based on this link? – RickS. Sep 15 '21 at 12:07
  • That was more about the `InterpolationMode` and `PixelOffsetMode` (and the GDI+ documentation). Everything that is done there is also done, in C# code, in the first post I linked. Zoom and Scale Factor are the exact same thing: a scale applied to one or more shapes. – Jimi Sep 15 '21 at 12:51

0 Answers0