Reputation: 115
I had to start making a jigsaw/image puzzle today in class, which went fine, except for the fact that my image kept/keeps re-scaling by itself.
The image itself is 300*300, but when running the code it becomes 192*192, even though I use the image's own size to declare the size.
The code consists of:
public partial class Form1 : Form
{
private Bitmap Bmp;
private Point BmpLoc;
int x = 0, y = 0;
public Form1()
{
InitializeComponent();
this.Paint += new System.Windows.Forms.PaintEventHandler(Form1_Paint);
}
private void showButton_Click(object sender, EventArgs e)
{
Bmp = new Bitmap("C:\\Users\\Admin\\Desktop\\img.png");
BmpLoc = new Point(0, 0);
Rectangle R = new Rectangle(BmpLoc, Bmp.Size);
int noot = Bmp.Size.Height;
label3.Text = noot.ToString();
this.Invalidate(R);
}
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
if (Bmp != null)
{
e.Graphics.DrawImage(Bmp, BmpLoc);
}
}
As you can see, it takes the Bitmap Size for the rectangle's size, so shouldn't it just show 300*300?
Thank you in advance for answering
Upvotes: 1
Views: 108
Reputation: 18463
This is because of the DPI of the image. You can use Graphics.DrawImageUnscaled
:
e.Graphics.DrawImageUnscaled(Bmp, BmpLoc);
You can check the DPI (resolution) of the image using HorizontalResolution
and VerticalResolution
properties. Usually the screen has a resolution of 96 DPI (dots per inch). However this is configurable in windows. If the resolution of the image is something other that this, the image is scaled to maintain its screen size.
Upvotes: 4