How to read tiff in C# and crop it


/ Published in: C#
Save to your folder(s)

Sample code to read tiff in C# and cropp it


Copy this code and paste it in your HTML
  1. Bitmap comments = null;
  2. string input = "somepath";
  3. // Open a Stream and decode a TIFF image
  4. using (Stream imageStreamSource = new FileStream(input, FileMode.Open, FileAccess.Read, FileShare.Read))
  5. {
  6. TiffBitmapDecoder decoder = new TiffBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
  7.  
  8. BitmapSource bitmapSource = decoder.Frames[0];
  9. using (Bitmap b = BitmapFromSource(bitmapSource))
  10. {
  11. Rectangle cropRect = new Rectangle(169, 1092, 567, 200);
  12. comments = new Bitmap(cropRect.Width, cropRect.Height);
  13.  
  14. //first cropping
  15. using (Graphics g = Graphics.FromImage(comments))
  16. {
  17. g.DrawImage(b, new Rectangle(0, 0, comments.Width, comments.Height),
  18. cropRect,
  19. GraphicsUnit.Pixel);
  20. }
  21. }
  22. }

URL: http://caringprogrammer.blogspot.com/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.