Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <Canvas x:Name="ImageCanvas"
- Grid.Column="1"
- Background="Transparent">
- <Image x:Name="SelectedPartImage"
- Width="{Binding ElementName=ImageCanvas, Path=ActualWidth}"
- Height="{Binding ElementName=ImageCanvas, Path=ActualHeight}"
- Panel.ZIndex="0"
- MouseLeftButtonDown="OnMouseLeftButtonDown"
- Source="{Binding SelectedPartImageSource}" />
- <Ellipse x:Name="EllipseClick"
- Width="15"
- Height="15"
- Panel.ZIndex="1"
- Fill="{StaticResource GreenColor}"
- Visibility="Hidden" />
- </Canvas>
- // ActualWidth and ActualHeight are the values of the image shown.
- RenderTargetBitmap rtb = new RenderTargetBitmap(
- (int)ActualWidth,
- (int)ActualHeight,
- 96d,
- 96d,
- System.Windows.Media.PixelFormats.Default);
- rtb.Render(ImageCanvas);
- PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
- pngEncoder.Frames.Add(BitmapFrame.Create(rtb));
- System.IO.MemoryStream ms = new System.IO.MemoryStream();
- pngEncoder.Save(ms);
- ms.Close();
- var bitmap = new BitmapImage();
- bitmap.BeginInit();
- bitmap.StreamSource = ms;
- bitmap.CacheOption = BitmapCacheOption.OnLoad;
- bitmap.EndInit();
- System.IO.File.WriteAllBytes("test.png", ms.ToArray());
- public void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- DrawEllipse();
- Point point = Mouse.GetPosition(SelectedPartImage);
- if (!(point.ConvertWPFPointToImagePoint(
- SelectedPartImageSource.PixelWidth,
- SelectedPartImageSource.PixelHeight,
- SelectedPartImage.ActualWidth,
- SelectedPartImage.ActualHeight))
- {
- return;
- }
- SelectPositionSuccess = CheckAlpha((int)point.X, (int)point.Y);
- EllipseClick.Visibility = SelectPositionSuccess ? Visibility.Visible : Visibility.Hidden;
- if (SelectPositionSuccess)
- {
- //Saving the Canvas
- RenderTargetBitmap rtb = new RenderTargetBitmap(
- (int)ViewModel.SelectedPartImageSource.Width,
- (int)ViewModel.SelectedPartImageSource.Height,
- 96d,
- 96d,
- System.Windows.Media.PixelFormats.Default);
- rtb.Render(ImageCanvas);
- //endcode as PNG
- PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
- pngEncoder.Frames.Add(BitmapFrame.Create(rtb));
- //save to memory stream
- System.IO.MemoryStream ms = new System.IO.MemoryStream();
- pngEncoder.Save(ms);
- ms.Close();
- var bitmap = new BitmapImage();
- bitmap.BeginInit();
- bitmap.StreamSource = ms;
- bitmap.CacheOption = BitmapCacheOption.OnLoad;
- bitmap.EndInit();
- System.IO.File.WriteAllBytes("test.png", ms.ToArray());
- }
- }
- public static Point ConvertWPFPointToDermaPoint(
- this Point inPoint,
- double inPixelWidth,
- double inPixelHeight,
- double inActualWidth,
- double inActualHeight)
- {
- // Width Ratio
- inPoint.X *= inPixelWidth / inActualWidth;
- // X
- if ((int)inPoint.X > inPixelWidth - 1)
- {
- inPoint.X = inPixelWidth - 1;
- }
- else if (inPoint.X < 0)
- {
- inPoint.X = 0;
- }
- // Height Ratio
- inPoint.Y *= inPixelHeight / inActualHeight;
- // Y
- if ((int)inPoint.Y > inPixelHeight - 1)
- {
- inPoint.Y = inPixelHeight - 1;
- }
- else if (inPoint.Y < 0)
- {
- inPoint.Y = 0;
- }
- return new Point(inPoint.X, inPoint.Y);
- }
Add Comment
Please, Sign In to add comment