2012-11-17 68 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; 
}