我想调整文件上传器中的图像大小。为此我被提到以下内容:
How to set Bitmap.Width and Bitmap.height
(Javed Akram的回答)
制作以下代码:
Dim imgSmall As Bitmap = New Bitmap(FUpload.PostedFile.InputStream, False)
imgSmall.Size = New Size(250, 300)
但是给我错误:
Size is read-only property.
在这种情况下如何调整图像大小?
答案 0 :(得分:2)
它是只读的;你必须从它创建一个新的位图。试试这个: -
internal static Image ScaleByPercent(Image image, Size size, float percent)
{
int sourceWidth = image.Width,
sourceHeight = image.Height;
int destWidth = (int)(sourceWidth * percent),
destHeight = (int)(sourceHeight * percent);
if (destWidth <= 0)
{
destWidth = 1;
}
if (destHeight <= 0)
{
destHeight = 1;
}
var resizedImage = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
resizedImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
// get handle to new bitmap
using (var graphics = Graphics.FromImage(resizedImage))
{
InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
// create a rect covering the destination area
var destRect = new Rectangle(0, 0, destWidth, destHeight);
var brush = new SolidBrush(drawing.Color.White);
graphics.FillRectangle(brush, destRect);
// draw the source image to the destination rect
graphics.DrawImage(image,
destRect,
new Rectangle(0, 0, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
}
return resizedImage;
}
这来自我制作的网站;如果你愿意我可以发送代码,以便在调整大小时确定如何保持正确的宽高比(即在'size'参数中传递的内容)
希望有所帮助