2010-06-28 40 views
1

搜索后,我发现这个代码:适应asp.net代码来调整图像

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进行检查。

正如你所看到的,我对System.Drawing一无所知,所以我需要一个“硬”的帮助来解决这个工作。

+0

ImageBuilder.Current.Build(sourcePathOrStream,destPathOrStream,new ResizeSettings(“maxwidth = 1024&maxheight = 768”))'//这是使用免费的http://imageresizing.net库的一行代码。并且可以避免[图像调整大小陷阱](http://nathanaeljones.com/163/20-image-resizing-pitfalls/)。 – 2011-06-21 23:59:58

回答

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

0

第一个问题:

昏暗newImage作为图片= Image.FromFile( “SampImag.jpg”)

第二个问题:

构建一个将返回基于一个尺寸对象的私有方法给定图像的原始尺寸对象。如果您愿意,您还可以添加“保持比例”标志。