Presentation Framework Imaging Classes Gray-Scale Example (loading, drawing, encoding)


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

You must include these DLL references in your vsweb or vscode project:\r\nPresentationCore,\r\nPresentationFramework,\r\nWindowsBase. \r\nNotice how I am manipulating the RGB values of each pixel, this is the power of these classes. The Drawing classes have the pixel drawing capabilities to draw lines and such already written for you, but handling a grayscale byte for byte is faster than relying on their other classes. I also chose to load and encode as PNG, but they have loaders and encoders for other file types as well that work the same way.


Copy this code and paste it in your HTML
  1. using System;
  2. using System.IO;
  3. using System.Windows.Media.Imaging;
  4. using System.Windows.Media;
  5.  
  6. namespace MyNamespace.Imaging
  7. {
  8. public sealed partial class MyImageClass
  9. {
  10. public enum eBpp : byte { _1 = 0, _8 = 1, _16 = 2, _24 = 3, _32 = 4, _40 = 5, _48 = 6, _56 = 7, _64 = 8, _128 = 16 }
  11.  
  12. protected static BitmapSource LoadPng(string fileName)
  13. {
  14. FileStream fs = File.OpenRead(fileName);
  15. byte[] data = new byte[fs.Length];
  16. fs.Read(data, 0, (int)fs.Length);
  17. fs.Close();
  18. MemoryStream ms = new MemoryStream(data);
  19. PngBitmapDecoder decoder = new PngBitmapDecoder(ms, BitmapCreateOptions.None, BitmapCacheOption.None);
  20. return decoder.Frames[0];
  21. }
  22.  
  23. protected static void MakeGrayScale(ref BitmapSource image)
  24. {
  25. byte bytes = (byte)(eBpp)Enum.Parse(typeof(eBpp), "_" + image.Format.BitsPerPixel);
  26. int stride = image.PixelWidth * bytes;
  27. long size = image.PixelHeight * stride;
  28. byte[] source = new byte[size];
  29. image.CopyPixels(source, stride, 0);
  30. byte[] dest = new byte[size];
  31. if (image.Format.BitsPerPixel == 32)
  32. {
  33. for (int i = bytes - 1; i < size; i += bytes)
  34. {
  35. uint avrg = (uint)source[i - 3] + (uint)source[i - 2] + (uint)source[i - 1];
  36. if (avrg != 0)
  37. avrg = avrg / 3;
  38.  
  39. dest[i - 3] = (byte)avrg;
  40. dest[i - 2] = (byte)avrg;
  41. dest[i - 1] = (byte)avrg;
  42. dest[i] = source[i]; //alpha
  43. }
  44. } //aren't handling any other types than 32bit color, but these can be added easily
  45.  
  46. image = BitmapSource.Create(image.PixelWidth, image.PixelHeight, 96, 96, image.Format, BitmapPalettes.Halftone256Transparent, dest, stride);
  47. }
  48.  
  49. protected static MemoryStream PngEncode(BitmapSource image)
  50. {
  51. //encode image as png
  52. PngBitmapEncoder encoder = new PngBitmapEncoder();
  53. encoder.Interlace = PngInterlaceOption.On;
  54. encoder.Frames.Add(BitmapFrame.Create(image));
  55. MemoryStream ret = new MemoryStream(); //create valid return stream
  56. encoder.Save(ret);
  57. ret.Position = 0;
  58. return ret;
  59. }
  60. }
  61. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.