2010-06-28 47 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」)

第二個問題:

構建一個將返回基於一個尺寸對象的私有方法給定圖像的原始尺寸對象。如果您願意,您還可以添加「保持比例」標誌。