我正在寻找一个例程,通过它我可以裁剪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 :(得分: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;
}