Skip to main content
 首页 » 编程设计

c#之为什么我的图像会自动重新缩放

2024年02月20日11freeliver54

今天我不得不在类里面开始制作拼图/图像拼图,一切顺利,除了我的图像不断/不断自行重新缩放这一事实。

图像本身是 300*300,但是当运行代码时它变成了 192*192,即使我使用图像自己的尺寸来声明尺寸。

代码组成:

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); 
        } 
    } 

如您所见,它采用位图大小作为矩形的大小,所以它不应该只显示 300*300 吗?

预先感谢您的回答

请您参考如下方法:

这是因为图像的 DPI。您可以使用 Graphics.DrawImageUnscaled :

e.Graphics.DrawImageUnscaled(Bmp, BmpLoc); 

您可以使用 HorizontalResolution 检查图像的 DPI(分辨率)和 VerticalResolution特性。通常屏幕的分辨率为 96 DPI(每英寸点数)。但是,这在 Windows 中是可配置的。如果图像的分辨率不是这个分辨率,图像将被缩放以保持其屏幕大小。