2009-07-06 2 views
0

ユーザーがデジタルカメラから画像をアップロードできる、かなり単純なASP.NETアプリケーションです。私はそれをウェブとサムネイル用に実行可能なサイズにリサイズする必要があります。 ここには何がベストプラクティスですか? Webサーバーに何かをインストールせずに簡単に実装できるライブラリはありますか?ASP.NETプロジェクトでユーザーがアップロードしたイメージのサイズ変更 - ライブラリですか?

+0

System.Drawing名前空間のクラスで実装されていない機能がありますか? –

+0

System.Drawingはひどくバグです。 [あなたがダイビングする前に28個の画像サイズ変更の落とし穴のリストを読んでください](http://nathanaeljones.com/163/20-image-resizing-pitfalls/)私は[imageresizing.netライブラリ](http:// imageresizing .net /)あなたが探しているものを正確に行う。シンプルで、1行のAPIを備えています。 –

答えて

0

BitMap classを参照してください。これは、コンストラクタでサイズを指定することで簡単に行うことができます。

だから、あなたが半分に望んでいたと想像:

Bitmap firstBitMap; // created somewhere else 

// Create a new bitmap from the first, scaling down as we go 
Bitmap halfSize = new Bitmap(firstBitMap, new Size(firstBitMap.Width/2, firstBitMap.Height/2)); 

あなたはより高品質のソリューションをしたい場合は、あなたがconsider the InterpolationMode enumerationする必要があります。

単純なシナリオでは、サードパーティのライブラリを気にする必要はありません。

0

私は画像の専門家でありませんが、私は、ウェブサイト上の画像リサイズを実施し、このようなものを使用:

public static void ResizeImageHighQuality(string imgFilename, string imgResizedFilename, int resizeH, int resizeW) 
{ 
    Image img = Image.FromFile(imgFilename); 
    int h = 0, w = 0; 

    if (img.Width > img.Height) 
    { 
     w = Convert.ToInt32(resizeW); 
     h = Convert.ToInt32(w * Convert.ToDouble(img.Height)/Convert.ToDouble(img.Width)); 

    } 
    else if (img.Height > img.Width) 
    { 
     h = Convert.ToInt32(resizeH); 
     w = Convert.ToInt32(h * Convert.ToDouble(img.Width)/Convert.ToDouble(img.Height)); 
    } 
    else 
    { 
     h = resizeH; 
     w = resizeW; 
    } 

    Image thumbnail = new Bitmap(w, h); 
    Graphics graphic = Graphics.FromImage(thumbnail); 

    graphic.InterpolationMode = InterpolationMode.HighQualityBicubic; 
    graphic.SmoothingMode = SmoothingMode.HighQuality; 
    graphic.PixelOffsetMode = PixelOffsetMode.HighQuality; 
    graphic.CompositingQuality = CompositingQuality.HighQuality; 

    graphic.DrawImage(img, 0, 0, w, h); 

    ImageCodecInfo[] Info = ImageCodecInfo.GetImageEncoders(); 
    EncoderParameters Params = new EncoderParameters(1); 
    Params.Param[0] = new EncoderParameter(Encoder.Quality, 100L); 

    File.Delete(imgResizedFilename); 
    FileStream fs = new FileStream(imgResizedFilename, FileMode.CreateNew); 
    thumbnail.Save(fs, Info[1], Params); 
    fs.Close(); 
} 
1

免責条項:私はこの図書館の著者です。

しかし、この目的のために設計されており、成熟しており、十分にテストされています。

http://nathanaeljones.com/products/asp-net-image-resizer/

私はあなたが含まれているサンプル・アプリケーションは、あなたが必要とする正確に何を見つけるだろうと思います。

0

ビットマップ(およびその他のSystem.Drawingクラス)はspecifically prohibited in ASP.NETです(ドキュメントページの上部にある警告を参照してください)。このような例外になることがあり、それらを使用して

TypeInitializationException: The type initializer for 'System.Windows.Media.Brush' threw an exception. 
    at System.Windows.Media.SolidColorBrush..ctor(Color color) 
    ... 
'System.Windows.Media.Transform' threw an exception. 
    at System.Windows.Media.Brush..cctor()Win32Exception: The operation completed successfully 
    at MS.Win32.UnsafeNativeMethods.RegisterClassEx(WNDCLASSEX_D wc_d) 
    at MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32 exStyle, Int32 x, Int32 y, Int32 width, Int32 height, String name, IntPtr parent, HwndWrapperHook[] hooks) 
    at MS.Win32.MessageOnlyHwndWrapper..ctor() 
    at System.Windows.Threading.Dispatcher..ctor() 
    at System.Windows.Threading.Dispatcher.get_CurrentDispatcher() 
    at System.Windows.Freezable..ctor() 
    at System.Windows.Media.MatrixTransform..ctor(Matrix matrix) 
    at System.Windows.Media.Transform..cctor() 

Win32Exception: The operation completed successfully 
    at MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32 exStyle, Int32 x, Int32 y, Int32 width, Int32 height, String name, IntPtr parent, HwndWrapperHook[] hooks) 
    at System.Windows.Media.MediaContextNotificationWindow..ctor(MediaContext ownerMediaContext) 
    at System.Windows.Media.MediaContext..ctor(Dispatcher dispatcher) 

あなたがやろうとしている内容に応じて、Windows Imaging Componentはあなたのニーズを満たすことができます。私たちはまた、単一のSTAスレッドを作成し、すべての描画操作をそのスレッドに呼び出すことで幸運を得ました。

関連する問題