我想使用后面的代码重新调整asp:image的大小。我将根据它的高度重新调整图像的大小,以便它可以适应到位。到目前为止我已经
了 Image img1 = ((Image)e.Item.FindControl("prodImage"));
int finalWidth = Convert.ToInt32(img1.Width);
int maxWidth = 20;
if (finalWidth > maxWidth)
{
resize
}
else
{
dont resize
}
我收到转换错误,因为img1.width是一个网络单元。我试过但是不能把它作为一个int转换或者把int作为一个单元。
感谢任何可以提供帮助的人
答案 0 :(得分:1)
您可以使用此代码
double ratio = maxWidth / finalWidth;
int maxHeight = (int) img1.Height * ratio;
System.Drawing.Image resized= img1.GetThumbnailImage(maxWidth , maxHeight , null, IntPtr.Zero);
看看这个function
答案 1 :(得分:1)
请参阅“High Quality Image Scaling C#”中的回答。
以下是您需要的相关代码:
/// <summary>
/// Resize the image to the specified width and height.
/// </summary>
/// <param name="image">The image to resize.</param>
/// <param name="width">The width to resize to.</param>
/// <param name="height">The height to resize to.</param>
/// <returns>The resized image.</returns>
public static System.Drawing.Bitmap ResizeImage(System.Drawing.Image image, int width, int height)
{
//a holder for the result
Bitmap result = new Bitmap(width, height);
//use a graphics object to draw the resized image into the bitmap
using (Graphics graphics = Graphics.FromImage(result))
{
//set the resize quality modes to high quality
graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//draw the image into the target bitmap
graphics.DrawImage(image, 0, 0, result.Width, result.Height);
}
//return the resulting bitmap
return result;
}
答案 2 :(得分:0)
我发现如何在不使用图形库的情况下进行操作
//Create a new image and set it to the image that was loaded in initially.
System.Web.UI.WebControls.Image image = loaded image;
//Check if the image has a picture and if not put in the no_picture image.
if (product.Image1.ToString() == "")
{
((System.Web.UI.WebControls.Image)e.Item.FindControl("prodImage")).ImageUrl = "no_picture.gif";
}
else
{
((System.Web.UI.WebControls.Image)e.Item.FindControl("prodImage")).ImageUrl = "upload" + product.Image1.ToString();
}
//Call a function to keep the images within the borders of each repeater cell. Returns an Image object so we can access the new height/width.
System.Web.UI.WebControls.Image dummyImg = keepAspectRatio(image, 110, 100);
image.Width = new Unit(dummyImg.Width.Value);
image.Height = new Unit(dummyImg.Height.Value);
public System.Web.UI.WebControls.Image keepAspectRatio(System.Web.UI.WebControls.Image image, double maxWidth, double maxHeight)
{
//Create an Image object to store width and height
System.Web.UI.WebControls.Image dummyImage2 = new System.Web.UI.WebControls.Image();
//Ratio variables to calculate aspect
double MaxRatio = maxWidth / maxHeight;
double ImgRatio = image.Width.Value / image.Height.Value;
//Compare the images width that was passed in to the max width allowed
if (image.Width.Value > maxWidth)
{
//Set the dummy images height and width
dummyImage2.Height = (int) Math.Round(maxWidth / ImgRatio, 0);
dummyImage2.Width = (int)maxWidth;
}
//Compare the images height that was passed in to the max height allowed
if (image.Height.Value > maxHeight)
{
//Set the dummy images height and width
dummyImage2.Height = (int)Math.Round(maxWidth * ImgRatio, 0);
dummyImage2.Width = (int)maxHeight;
}
//Returnthe dummy object so its parameters can be accessed
return dummyImage2;
}
答案 3 :(得分:0)
您可以使用此功能来保持比率:
System.Drawing.Size size = LockAspectRatio(sFullFilePathAndName, Convert.ToInt32(rdPhoto.Width.Value) - 20, Convert.ToInt32(rdPhoto.Height.Value) - 80);
imgRep.Width = new Unit(size.Width);
imgRep.Height = new Unit(size.Height);
public System.Drawing.Size LockAspectRatio(string sFilePath, double maxWidth, double maxHeight)
{
int newWidth, newHeight;
System.Drawing.Size size = new System.Drawing.Size();
System.Drawing.Image image = System.Drawing.Image.FromFile(sFilePath);
size.Width = image.Width;
size.Height = image.Height;
newWidth = image.Width;
newHeight = image.Height;
double imgRatio = (double)image.Width / (double)image.Height;
//Step 1: If image is bigger than container, shrink it
if (image.Width > maxWidth)
{
size.Height = (int)Math.Round(maxWidth * imgRatio, 0);
size.Width = (int)maxWidth;
newWidth = size.Width;
newHeight = size.Height;
}
if (newHeight > maxHeight)
{
size.Height = (int)maxHeight;
size.Width = (int)Math.Round(maxHeight * imgRatio, 0);
}
//Step 2: If image is smaller than container, stretch it (optional)
if ((image.Width < maxWidth) && (image.Height < maxHeight))
{
if (image.Width < maxWidth)
{
size.Height = (int)Math.Round(maxWidth * imgRatio, 0);
size.Width = (int)maxWidth;
newWidth = size.Width;
newHeight = size.Height;
}
if (newHeight > maxHeight)
{
size.Height = (int)maxHeight;
size.Width = (int)Math.Round(maxHeight * imgRatio, 0);
}
}
return size;
}