2009-08-19 10 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

Davidに感謝します。また、画像のサイズ変更(縦横比)を扱うための同様のコードを書いています。私が直面している問題は、160pxから110px、48pxから48pxのイメージという、サイズ変更をしたいときです。結果は良くありません。解決策があるかどうかは不明ですが、サードパーティベンダーがこの問題を解決できるかどうかは考えました。 ありがとうございます。 –

+0

System.Drawing.ImageはAPIです –

+1

問題を解決するには、イメージのサイズが大きく、縮尺と切り抜きがどのようなものかを調べる必要があります。 System.Drawing提供する:D –

0

は、私が提供する他の回答に代わるものとしてこれを提供します。 Image Magickは、.netから使用できる非常に強力で成熟した画像処理ライブラリです。私はそれで多くの成功を収めてきました。

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

+0

.netライブラリは開発されていません。 p/invokeの使い方が分からない限り、何の意味もありません –

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

優秀!私はそれを試してみましょう。 –

+1

また、このサイトをチェックアウトします: http://nathanaeljones.com/products/asp-net-image-resizer/ –

+0

私は彼のソフトウェアを以前見たことがあります。コンテンツを公開するときにイメージをプッシュする方が良い。 DDoS攻撃を防止する。 –

2

ImageResizerライブラリは積極的に(2007年)、開発、保守、およびサポートされています。最新のパフォーマンス技術、機能を備えており、シンプルなAPIを備えています。それは安全で、安全で、信頼性があり、何百もの商用ウェブサイトの力を発揮します。

ここは、フル画像サイズ変更サンプルアプリケーションで4.0、MVCを通じてASP.NET 2.0との互換性だ、と

関連する問題