2011-01-11 15 views
4

を定義することで、.NET C#でサムネイルを作成します。私はサムネイル作成のために、私のウェブサイトに次のコードを使用しています幅

string furl = "~/images/thumbs/" + matchString; 
lBlogThumb.ImageUrl = GetThumbnailView(furl, 200, 200); 


private string GetThumbnailView(string originalImagePath, int height, int width) 
     { 
      //Consider Image is stored at path like "ProductImage\\Product1.jpg" 

      //Now we have created one another folder ProductThumbnail to store thumbnail image of product. 
      //So let name of image be same, just change the FolderName while storing image. 
      string thumbnailImagePath = originalImagePath.Replace("thumbs", "thumbs2"); 
      //If thumbnail Image is not available, generate it. 
      if (!System.IO.File.Exists(Server.MapPath(thumbnailImagePath))) 
      { 
       System.Drawing.Image imThumbnailImage; 
       System.Drawing.Image OriginalImage = System.Drawing.Image.FromFile(Server.MapPath(originalImagePath)); 
       imThumbnailImage = OriginalImage.GetThumbnailImage(width, height, 
          new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero); 

       imThumbnailImage.Save(Server.MapPath(thumbnailImagePath), System.Drawing.Imaging.ImageFormat.Jpeg); 

       imThumbnailImage.Dispose(); 
       OriginalImage.Dispose(); 
      } 
      return thumbnailImagePath; 
     } 

public bool ThumbnailCallback() { return false; } 

私はこのコードを変更したい、と幅を定義し、サムネイルを作成することができるだろうのみ。私が頭に浮かべているのは、実際には画像の切り抜き/サイズ変更、静的な幅の使用、比率の維持などです。それは可能ですか?

+1

が、これはあなたが何をしたいですか? newHeight = oldHeight * newWidth/oldWidth、画像をトリミングすると言ったときの意味がわからない - トリミングしてサイズを変更したいですか? –

+0

あなたの助けてくれてありがとう – zekia

+0

idkなぜかこれは私に "メモリ不足"誤って..これのためにexeptionを置くことができる人は誰ですか?私は本当にC#がどのようにメモリを扱うのか知りません。 –

答えて

7

originalWidth =元の画像の幅とthumbWidthがあります。 thumbHeigth=originalHeigth*thumbWidth/originalWidth

+0

ご協力いただきありがとうございます – zekia

0

サムネイルの幅が140で、ファイルの下に丸めを入れない基本的な例は、ASP.Net FileUploadコントロールからアップロードされたHttpPostedFile、HttpPostedFileストリームを公開します。

// Save images to disk. 
using (System.Drawing.Image image = System.Drawing.Image.FromStream(file.InputStream)) 
using (System.Drawing.Image thumbnailImage = image.GetThumbnailImage(140, Convert.ToInt32((image.Height/(image.Width/140))), null, IntPtr.Zero)) 
{ 
    if (image != null) 
    { 
     image.Save(imageFilePath); 
     thumbnailImage.Save(thumbnailImagePath); 
    } 
    else 
     throw new ArgumentNullException("Image stream is null"); 
} 
15

サイズ変更とトリミングについて言及します。サムネイルの高さを固定幅で変化させたい場合は、すでに提供されている回答が有効です。

トリミングについて言えば、固定されたサムネイルサイズが必要な場合があります。幅が埋められ、オーバーフローする垂直部分が切り取られます。そのような場合は、もう少し作業を行う必要があります。私は最近同様のものが必要でした。これが私が思いついたものです。

これは、ソースイメージがターゲットサムネイルを完全に埋めるようにサイズが変更され、トリミングされたオリジナルのサムネイルを作成し、オーバーフローをトリミングします。元のアスペクト比とサムネイルのアスペクト比が異なる場合でも、サムネイル内に境界線はありません。

public System.Drawing.Image CreateThumbnail(System.Drawing.Image image, Size thumbnailSize) 
{ 
    float scalingRatio = CalculateScalingRatio(image.Size, thumbnailSize); 

    int scaledWidth = (int)Math.Round((float)image.Size.Width * scalingRatio); 
    int scaledHeight = (int)Math.Round((float)image.Size.Height * scalingRatio); 
    int scaledLeft = (thumbnailSize.Width - scaledWidth)/2; 
    int scaledTop = (thumbnailSize.Height - scaledHeight)/2; 

    // For portrait mode, adjust the vertical top of the crop area so that we get more of the top area 
    if (scaledWidth < scaledHeight && scaledHeight > thumbnailSize.Height) 
    { 
     scaledTop = (thumbnailSize.Height - scaledHeight)/4; 
    } 

    Rectangle cropArea = new Rectangle(scaledLeft, scaledTop, scaledWidth, scaledHeight); 

    System.Drawing.Image thumbnail = new Bitmap(thumbnailSize.Width, thumbnailSize.Height); 
    using (Graphics thumbnailGraphics = Graphics.FromImage(thumbnail)) 
    { 
     thumbnailGraphics.CompositingQuality = CompositingQuality.HighQuality; 
     thumbnailGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic; 
     thumbnailGraphics.SmoothingMode = SmoothingMode.HighQuality; 
     thumbnailGraphics.DrawImage(image, cropArea); 
    } 
    return thumbnail; 
} 

private float CalculateScalingRatio(Size originalSize, Size targetSize) 
{ 
    float originalAspectRatio = (float)originalSize.Width/(float)originalSize.Height; 
    float targetAspectRatio = (float)targetSize.Width/(float)targetSize.Height; 

    float scalingRatio = 0; 

    if (targetAspectRatio >= originalAspectRatio) 
    { 
     scalingRatio = (float)targetSize.Width/(float)originalSize.Width; 
    } 
    else 
    { 
     scalingRatio = (float)targetSize.Height/(float)originalSize.Height; 
    } 

    return scalingRatio; 
} 

あなたのコードで使用するには、あなたはこれでOriginalImage.GetThumbnailImageにお電話を置き換えることができます:ポートレート画像のために、このコードは実際には、元の画像の上にわずかに高いサムネイルのビューポートをシフトすること

imThumbnailImage = CreateThumbnail(OriginalImage, new Size(width, height)); 

注意。これはサムネイルが作成されたときに、人物のポートレートショットが頭のないトルソにならないように行われました。そのロジックが欲しくない場合は、 "ポートレートモード"のコメントに続いてifブロックを削除するだけです。

関連する問題