Guest User

Untitled

a guest
May 23rd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. <Canvas x:Name="ImageCanvas"
  2. Grid.Column="1"
  3. Background="Transparent">
  4.  
  5. <Image x:Name="SelectedPartImage"
  6. Width="{Binding ElementName=ImageCanvas, Path=ActualWidth}"
  7. Height="{Binding ElementName=ImageCanvas, Path=ActualHeight}"
  8. Panel.ZIndex="0"
  9. MouseLeftButtonDown="OnMouseLeftButtonDown"
  10. Source="{Binding SelectedPartImageSource}" />
  11.  
  12. <Ellipse x:Name="EllipseClick"
  13. Width="15"
  14. Height="15"
  15. Panel.ZIndex="1"
  16. Fill="{StaticResource GreenColor}"
  17. Visibility="Hidden" />
  18. </Canvas>
  19.  
  20. // ActualWidth and ActualHeight are the values of the image shown.
  21. RenderTargetBitmap rtb = new RenderTargetBitmap(
  22. (int)ActualWidth,
  23. (int)ActualHeight,
  24. 96d,
  25. 96d,
  26. System.Windows.Media.PixelFormats.Default);
  27.  
  28. rtb.Render(ImageCanvas);
  29.  
  30. PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
  31. pngEncoder.Frames.Add(BitmapFrame.Create(rtb));
  32.  
  33. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  34. pngEncoder.Save(ms);
  35. ms.Close();
  36.  
  37. var bitmap = new BitmapImage();
  38. bitmap.BeginInit();
  39. bitmap.StreamSource = ms;
  40. bitmap.CacheOption = BitmapCacheOption.OnLoad;
  41. bitmap.EndInit();
  42.  
  43. System.IO.File.WriteAllBytes("test.png", ms.ToArray());
  44.  
  45. public void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  46. {
  47. DrawEllipse();
  48.  
  49. Point point = Mouse.GetPosition(SelectedPartImage);
  50.  
  51. if (!(point.ConvertWPFPointToImagePoint(
  52. SelectedPartImageSource.PixelWidth,
  53. SelectedPartImageSource.PixelHeight,
  54. SelectedPartImage.ActualWidth,
  55. SelectedPartImage.ActualHeight))
  56. {
  57. return;
  58. }
  59.  
  60. SelectPositionSuccess = CheckAlpha((int)point.X, (int)point.Y);
  61.  
  62. EllipseClick.Visibility = SelectPositionSuccess ? Visibility.Visible : Visibility.Hidden;
  63.  
  64. if (SelectPositionSuccess)
  65. {
  66. //Saving the Canvas
  67. RenderTargetBitmap rtb = new RenderTargetBitmap(
  68. (int)ViewModel.SelectedPartImageSource.Width,
  69. (int)ViewModel.SelectedPartImageSource.Height,
  70. 96d,
  71. 96d,
  72. System.Windows.Media.PixelFormats.Default);
  73.  
  74. rtb.Render(ImageCanvas);
  75.  
  76. //endcode as PNG
  77. PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
  78. pngEncoder.Frames.Add(BitmapFrame.Create(rtb));
  79.  
  80. //save to memory stream
  81. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  82. pngEncoder.Save(ms);
  83. ms.Close();
  84.  
  85. var bitmap = new BitmapImage();
  86. bitmap.BeginInit();
  87. bitmap.StreamSource = ms;
  88. bitmap.CacheOption = BitmapCacheOption.OnLoad;
  89. bitmap.EndInit();
  90.  
  91. System.IO.File.WriteAllBytes("test.png", ms.ToArray());
  92. }
  93. }
  94.  
  95. public static Point ConvertWPFPointToDermaPoint(
  96. this Point inPoint,
  97. double inPixelWidth,
  98. double inPixelHeight,
  99. double inActualWidth,
  100. double inActualHeight)
  101. {
  102. // Width Ratio
  103. inPoint.X *= inPixelWidth / inActualWidth;
  104.  
  105. // X
  106. if ((int)inPoint.X > inPixelWidth - 1)
  107. {
  108. inPoint.X = inPixelWidth - 1;
  109. }
  110. else if (inPoint.X < 0)
  111. {
  112. inPoint.X = 0;
  113. }
  114.  
  115. // Height Ratio
  116. inPoint.Y *= inPixelHeight / inActualHeight;
  117.  
  118. // Y
  119. if ((int)inPoint.Y > inPixelHeight - 1)
  120. {
  121. inPoint.Y = inPixelHeight - 1;
  122. }
  123. else if (inPoint.Y < 0)
  124. {
  125. inPoint.Y = 0;
  126. }
  127.  
  128. return new Point(inPoint.X, inPoint.Y);
  129. }
Add Comment
Please, Sign In to add comment