2012-11-17 67 views
0

我一直在尋找一個可以裁剪tiff圖像的例程,並且我知道了它,但它會產生很多錯誤。例程如下:如何在asp.net中裁剪tiff圖像

Bitmap comments = null; 
string input = "somepath"; 
// Open a Stream and decode a TIFF image 
using (Stream imageStreamSource = new FileStream(input, FileMode.Open, FileAccess.Read, FileShare.Read)) 
{ 
TiffBitmapDecoder decoder = new TiffBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); 

BitmapSource bitmapSource = decoder.Frames[0]; 
using (Bitmap b = BitmapFromSource(bitmapSource)) 
{ 
Rectangle cropRect = new Rectangle(169, 1092, 567, 200); 
comments = new Bitmap(cropRect.Width, cropRect.Height); 

//first cropping 
using (Graphics g = Graphics.FromImage(comments)) 
{ 
g.DrawImage(b, new Rectangle(0, 0, comments.Width, comments.Height), 
cropRect, 
GraphicsUnit.Pixel); 
} 
} 
} 

當我嘗試編譯它時,出現錯誤。我試圖添加引用到許多搜索谷歌程序集,但無法解決它。我從這個網址得到了這段代碼:

http://snipplr.com/view/63053/ 

我在尋找建議。

+0

它肯定有助於增加您得到的錯誤.... – rene

+0

此外,請提供有關TIFF圖像本身(彩色,多幀等)的更多信息。開箱即用的GDI +/.NET在某些類型的TIFF文件中遇到了麻煩。 – HackedByChinese

回答

0

TiffBitmapDecoder類是從Presentation.Core換句話說它來自WPF。

BitmapFromSource不是任何.net框架類的方法。您可以使用此代碼將BitmapSource轉換爲位圖。

private Bitmap BitmapFromSource(BitmapSource bitmapsource) 
{ 
    Bitmap bitmap; 
    using (MemoryStream outStream = new MemoryStream()) 
    { 
     BitmapEncoder encoder = new BmpBitmapEncoder(); 
     encoder.Frames.Add(BitmapFrame.Create(bitmapsource)); 
     encoder.Save(outStream); 
     bitmap = new Bitmap(outStream); 
    } 
    return bitmap; 
}