简体   繁体   English

根据设备的宽度和高度调整图像大小

[英]resize an image based on the devices width and height

I am trying to resize an image in asp.net Visual studio V2008, c# 我正在尝试在ASP.NET Visual Studio V2008中调整图像大小,C#

using (Graphics newgraphics = Graphics.FromImage(myBackgroundImage))
{
newgraphics.Clear(Color.FromArgb(-1));
if ((float)oldbmp.Width / (float)Width == (float)oldbmp.Height / (float)Height)            
  {
    newgraphics.DrawImage(oldbmp, 0, 0, Width, Height);
  }
} 

I have entered the above code, but I get the following errors on a build. 我已经输入了上面的代码,但是在构建时出现以下错误。

the Graphic.DrawImage is overloaded. Graphic.DrawImage重载。 It is expecting , int, int, Rectangle, graphicsunit 期望,int,int,Rectangle,graphicsunit

Anyone have any suggestions? 有人有什么建议吗?

Much appreciated 非常感激
Rachael 雷切尔

Based on the error message you've provided, it seems like you are not providing the correct arguments for DrawImage method. 根据您提供的错误消息,似乎您没有为DrawImage方法提供正确的参数。

Can you confirm what type 'oldbmp' belongs to ? 您可以确认“ oldbmp”属于什么类型吗? It should be an 'Image' type in order to satisfy one of the overload requirements. 为了满足过载要求之一,它应该是“图像”类型。

Below is all the overloads for DrawImage method. 以下是DrawImage方法的所有重载。

http://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawimage(v=vs.110).aspx http://msdn.microsoft.com/zh-cn/library/system.drawing.graphics.drawimage(v=vs.110).aspx

Based on your code, below overload should do the job. 根据您的代码,下面的重载应该可以完成工作。

http://msdn.microsoft.com/en-us/library/2ab36azw(v=vs.110).aspx http://msdn.microsoft.com/zh-CN/library/2ab36azw(v=vs.110).aspx

Have you tried using 0.0F instead of 0 ? 您是否尝试使用0.0F而不是0?

I have solved this issue 我已经解决了这个问题

  • In InitializeComponent I added the following two lines 在InitializeComponent中,我添加了以下两行

  • \n        'this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);' 'this.AutoScaleDimensions = new System.Drawing.SizeF(96F,96F);'\n        'this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;' 'this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;'\n    
    This explains why I did that CLick Here 这解释了为什么我要点击这里
  • I attached the image to the form Skin - As it was currently being draw in the paint override method - The code above does not re-size images drawn in the paint override method 我将图像附加到皮肤窗体上-由于当前正在使用绘画覆盖方法绘制,因此-上面的代码不会调整在绘画覆盖方法中绘制的图像的大小

  • Set the sizemode to Stretch Image and then will check the DPI of the device and reset the image accordingly 将sizemode设置为Stretch Image,然后将检查设备的DPI并相应地重置图像
That should do the trick 这应该够了吧

Thanks jundev for your patient. 感谢jundev的耐心配合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM