Guest User

Untitled

a guest
Jun 19th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. class AliasedBitmapSource : BitmapSource
  2. {
  3. private Bitmap source;
  4. public AliasedBitmapSource(Bitmap source)
  5. {
  6. this.source = source;
  7. this.pixelHeight = source.Height;
  8. this.pixelWidth = source.Width;
  9. this.dpiX = source.HorizontalResolution;
  10. this.dpiY = source.VerticalResolution;
  11. }
  12.  
  13. public override event EventHandler DownloadCompleted;
  14. public override event EventHandler<ExceptionEventArgs> DownloadFailed;
  15. public override event EventHandler<ExceptionEventArgs> DecodeFailed;
  16.  
  17. protected override Freezable CreateInstanceCore()
  18. {
  19. throw new NotImplementedException();
  20. }
  21.  
  22. private readonly double dpiX;
  23. public override double DpiX
  24. {
  25. get
  26. {
  27. return dpiX;
  28. }
  29. }
  30.  
  31. private readonly double dpiY;
  32. public override double DpiY
  33. {
  34. get
  35. {
  36. return dpiY;
  37. }
  38. }
  39.  
  40. private readonly int pixelHeight;
  41. public override int PixelHeight
  42. {
  43. get
  44. {
  45. return pixelHeight;
  46. }
  47. }
  48.  
  49. private readonly int pixelWidth;
  50. public override int PixelWidth
  51. {
  52. get
  53. {
  54. return pixelWidth;
  55. }
  56. }
  57.  
  58. public override System.Windows.Media.PixelFormat Format
  59. {
  60. get
  61. {
  62. return PixelFormats.Bgra32;
  63. }
  64. }
  65.  
  66. public override BitmapPalette Palette
  67. {
  68. get
  69. {
  70. return null;
  71. }
  72. }
  73.  
  74. public unsafe override void CopyPixels(Int32Rect sourceRect, Array pixels, int stride, int offset)
  75. {
  76. BitmapData sourceData = source.LockBits(
  77. sourceRect.ToRectangle(),
  78. ImageLockMode.ReadWrite,
  79. System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  80.  
  81. fixed (byte* _ptr = &((byte[])pixels)[0])
  82. {
  83. byte* dstptr = _ptr;
  84. byte* srcptr = (byte*)sourceData.Scan0;
  85.  
  86. for (int i = 0; i < pixels.Length; ++i)
  87. {
  88. *dstptr = *srcptr;
  89. ++dstptr;
  90. ++srcptr;
  91. }
  92. }
  93.  
  94. source.UnlockBits(sourceData);
  95. }
  96. }
  97.  
  98. public static class Extensions
  99. {
  100. public static Rectangle ToRectangle(this Int32Rect me)
  101. {
  102. return new Rectangle(
  103. me.X,
  104. me.Y,
  105. me.Width,
  106. me.Height);
  107. }
  108.  
  109. public static Int32Rect ToInt32Rect(this Rectangle me)
  110. {
  111. return new Int32Rect(
  112. me.X,
  113. me.Y,
  114. me.Width,
  115. me.Height);
  116. }
  117. }
Add Comment
Please, Sign In to add comment