调整asp.net代码以调整图像大小

时间:2010-06-28 15:15:33

标签: asp.net vb.net image-processing image-resizing

搜索后,我发现了这段代码:

Public Sub ResizeImage(ByVal scaleFactor As Double, ByVal fromStream As Stream, ByVal toStream As Stream)
    Dim image__1 = System.Drawing.Image.FromStream(fromStream)
    Dim newWidth = CInt(image__1.Width * scaleFactor)
    Dim newHeight = CInt(image__1.Height * scaleFactor)
    Dim thumbnailBitmap = New System.Drawing.Bitmap(newWidth, newHeight)

    Dim thumbnailGraph = System.Drawing.Graphics.FromImage(thumbnailBitmap)
    thumbnailGraph.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality
    thumbnailGraph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
    thumbnailGraph.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic

    Dim imageRectangle = New System.Drawing.Rectangle(0, 0, newWidth, newHeight)
    thumbnailGraph.DrawImage(image__1, imageRectangle)
    thumbnailBitmap.Save(toStream, image__1.RawFormat)

    thumbnailGraph.Dispose()
    thumbnailBitmap.Dispose()
    image__1.Dispose()
End Sub

有两件事我无法“修改”以解决我的问题:

  1. 我不想传递流,但我更喜欢传递像C:\mysite\photo\myphoto.gif这样的路径。如何“转换”它以接受文件而不是流?
  2. 在这个函数中,我要传递一个“scale”值。但我更喜欢检查图像是否太大(例如> 1024x768),而不是将其调整为最大1024x768。如何使用System.Drawing进行检查。
  3. 正如你所看到的,我对System.Drawing一无所知,所以我需要一个“硬”帮助来解决这个问题。

2 个答案:

答案 0 :(得分:0)

第一个问题:

Dim newImage As Image = Image.FromFile(“SampImag.jpg”)

第二个问题:

构建一个私有方法,该方法将根据给定图像的原始Size对象返回Size对象。如果您愿意,也可以添加“保持比例”标记。

答案 1 :(得分:0)

这是我5年前做过的一些c#代码(它应该仍然有用,我希望因为应用程序从未被触及过)。我认为它确实需要它,但如果它更小,它不会将图像升级到1024x768。此代码仅确保如果它大于1024x768,它将按比例调整大小以适应这些维度:

const int maxWidth = 1024;
const int maxHeight = 768;
Image newImage = Image.FromFile("YourPicture.jpg");
double percentToShrink = -1;
if (newImage.Width >= newImage.Height)
{
    // Do we need to resize based on width?
    if (newImage.Width > maxWidth)
    {
        percentToShrink = (double)maxWidth / (double)newImage.Width;
    }
}
else
{
    // Do we need to resize based on width?
    if (newImage.Height > maxHeight )
    {
        percentToShrink = (double)maxHeight  / (double)newImage.Height;
    }
}

int newWidth = newImage.Width;
int newHeight = newImage.Height;

// So do we need to resize?
if (percentToShrink != -1)
{
    newWidth = (int)(newImage.Width * percentToShrink);
    newHeight = (int)(newImage.Height * percentToShrink);
}

// convert the image to a png and get a byte[]
MemoryStream imgStream = new MemoryStream();
Bitmap bmp = new Bitmap(newWidth, newHeight);
using (Graphics g = Graphics.FromImage(bmp))
{
    g.InterpolationMode =   System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    g.FillRectangle(System.Drawing.Brushes.White, 0, 0, newWidth, newHeight);
    g.DrawImage(newImage, 0, 0, newWidth, newHeight);
}

// This can be whatever format you need
bmp.Save(imgStream, System.Drawing.Imaging.ImageFormat.Png);
byte[] imgBinaryData = imgStream.ToArray();
imgStream.Dispose();

如果需要将其转换为VB.NET,可以使用C#到VB.NET转换器here