简体   繁体   中英

How to crop a tiff image in asp.net

I was looking for a routine by which I can crop tiff image and I got it but it gives many error. Here is the routine:

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);
}
}
}

When I try to compile it, I get an error. I tried adding references to many assemblies searching google but couldn't resolve it. I got this code from this url:

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

I am looking for advice.

TiffBitmapDecoder class is from Presentation.Core in another words it is from WPF.

BitmapFromSource isn't method of any .net framework class. You can convert BitmapSource to Bitmap using this code.

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;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM