2009-08-19 51 views

回答

0

调整图像大小是很简单并不需要的API。我为自己写了这个任务。这里有点代码让你开始走下去。

// get original image 
System.Drawing.Image orignalImage = Image.FromFile(originalPath); 
// create a new image at the desired size 
System.Drawing.Bitmap newImage = new Bitmap(450, 338); 
// create grpahics object to draw with 
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(newImage); 
//draw the new image 
g.DrawImage(orignalImage, r); 
// save the new image  
newImage.Save(System.IO.Path.Combine(OUTPUT_FOLDER_PATH , ImageName.Replace(" ", ""))); 
+0

谢谢大卫。我也写了类似的代码来处理图像大小调整(w /高宽比)。我面临的问题是当我想重新调整大小 - 160像素110像素到48像素48像素的图像。结果不好。不确定是否有解决方案,但认为第三方供应商会/可能已经解决了这个问题。 谢谢。 – 2009-08-19 13:54:05

+0

System.Drawing.Image是一个API – 2009-08-19 13:54:10

+1

要解决您的问题,您应该了解图像的哪个维度更大,比例和裁剪。 System.Drawing将提供:D – 2009-08-19 13:57:03

0

我将提供此代替其提供的答案。 Image Magick是一个非常强大和成熟的图像处理库,您可以使用.net。我已经取得了很多成功。

http://www.imagemagick.org/script/index.php

+0

.net库不再被开发。 除非你知道如何使用p/invoke – 2009-08-19 13:53:33

1

下面是我用什么:

internal static System.Drawing.Image FixedSize(System.Drawing.Image imgPhoto, int Width, int Height) 
{ 
    int sourceWidth = Convert.ToInt32(imgPhoto.Width); 
    int sourceHeight = Convert.ToInt32(imgPhoto.Height); 
    int sourceX = 0; 
    int sourceY = 0; 
    int destX = 0; 
    int destY = 0; 

    float nPercent = 0; 
    float nPercentW = 0; 
    float nPercentH = 0; 

    nPercentW = ((float)Width/(float)sourceWidth); 
    nPercentH = ((float)Height/(float)sourceHeight); 
    if (nPercentH < nPercentW) 
    { 
     nPercent = nPercentH; 
     destX = System.Convert.ToInt16((Width - 
      (sourceWidth * nPercent))/2); 
    } 
    else 
    { 
     nPercent = nPercentW; 
     destY = System.Convert.ToInt16((Height - 
      (sourceHeight * nPercent))/2); 
    } 

    int destWidth = (int)(sourceWidth * nPercent); 
    int destHeight = (int)(sourceHeight * nPercent); 

    Bitmap bmPhoto = new Bitmap(Width, Height, 
     PixelFormat.Format24bppRgb); 
    bmPhoto.SetResolution(imgPhoto.HorizontalResolution, 
     imgPhoto.VerticalResolution); 

    Graphics grPhoto = Graphics.FromImage(bmPhoto); 
    grPhoto.Clear(Color.Black); 
    grPhoto.InterpolationMode = 
     InterpolationMode.HighQualityBicubic; 

    grPhoto.DrawImage(imgPhoto, 
     new Rectangle(destX, destY, destWidth, destHeight), 
     new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), 
     GraphicsUnit.Pixel); 

    grPhoto.Dispose(); 
    return bmPhoto; 
} 

用法很简单:

System.Drawing.Image orignalImage = Image.FromFile(filePath); 
System.Drawing.Image resizedImage = FixedSize(originalImage, 640, 480); 
+0

好极了!我会尝试一下。 – 2009-08-19 14:05:45

+1

还有人检查这个站点: http://nathanaeljones.com/products/asp-net-image-resizer/ – 2009-08-19 14:06:21

+0

我以前见过他的软件。它更好地发布内容时推出图像。防止DDOS攻击。 – 2009-08-19 14:26:49

2

ImageResizer库正在积极开发,维护和支持(2007年起)。它是最新的性能技术和功能,并有一个简单的API。它安全,安全,可靠,为数百个商业网站提供支持。

它通过4.0 MVC与ASP.NET 2.0兼容,并设计了IIS 7