1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace WpfBitmapWB
{
/// <summary>
/// Logique d'interaction pour UserControl1.xaml
/// </summary>
public partial class UserControl1 : UserControl
{
private static WriteableBitmap writeableBitmap = null;
// et son infrastructure
private static Int32Rect rect;
private static int bytesPerPixel;
private static int stride;
private static int arraySize;
private static byte[] colorArray;
// infrastructure pour l'ellipse curseur
private static Int32Rect ellipseRect;
private static int ellipseSize;
private static byte[] colorEllipse;
// infrastructure pour l'ellipse jumelle "GOMME"
private static Int32Rect ellipseRectBack;
private static byte[] colorEllipseBack;
public UserControl1()
{
InitializeComponent();
writeableBitmap = new WriteableBitmap((int)this.chart.Width, (int)this.chart.Height, 96, 96, PixelFormats.Bgra32, null);
rect = new Int32Rect(0, 0, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight);
bytesPerPixel = (writeableBitmap.Format.BitsPerPixel + 7) / 8;
stride = writeableBitmap.PixelWidth * bytesPerPixel;
arraySize = stride * writeableBitmap.PixelHeight;
colorArray = new byte[arraySize];
// OPTION 1
//CreateBmpEllipse();
// OPTION 2-commentée
//CreatetBmpDrawingGroup();
//Init random source
random.Source = writeableBitmap;
}
private void chart_PreviewMouseMove(object sender, MouseEventArgs e)
{
Point hitPoint = e.GetPosition(this.random);
Rect cliquableRect = new Rect(0, 0,
this.random.Width - ellipseRect.Width - 1,
this.random.Height - ellipseRect.Height - 1);
if (e.LeftButton == MouseButtonState.Pressed && cliquableRect.Contains(hitPoint ))
{
ellipseRectBack = ellipseRect; // Memorise le previous rect
//ellipseRect.X = (int)hitPoint.X - ellipseRect.Width-1; // centrage par rapport position souris
//ellipseRect.Y = (int)hitPoint.Y - ellipseRect.Height-1;
ellipseRect.X = (int)hitPoint.X ;
ellipseRect.Y = (int)hitPoint.Y ;
UpdatePixels();
// Maj du control Image
random.Source = writeableBitmap;
}
}
private static void UpdatePixels()
{
// on "gomme"
writeableBitmap.WritePixels(ellipseRectBack, colorEllipseBack, stride, 0);
// track la position souris
writeableBitmap.WritePixels(ellipseRect, colorEllipse, stride, 0);
}
// OPTION 1
private void CreateBmpEllipse()
{
Pen stroke = new Pen(Brushes.Red, 2.0);
Rect horizBounds = new Rect(0, 0, 100, 60);
Rect vertBounds = new Rect(0, 0, 60, 100);
Point center = new Point(50, 30);
DrawingVisual dv = new DrawingVisual();
DrawingContext ctx = dv.RenderOpen();
ctx.PushOpacity(0.5);
ctx.DrawEllipse(Brushes.Yellow, stroke, center, horizBounds.Width / 2, horizBounds.Height / 2);
ctx.DrawEllipse(Brushes.DarkBlue, stroke, center, vertBounds.Width / 2, vertBounds.Height / 2);
ctx.Pop();
ctx.Close();
RenderTargetBitmap bmp = new RenderTargetBitmap( (int)vertBounds.Width, (int)vertBounds.Height, 96, 96, PixelFormats.Pbgra32);
bmp.Render(dv);
ellipseRect = new Int32Rect(0, 0, bmp.PixelWidth, bmp.PixelHeight);
bytesPerPixel = (bmp.Format.BitsPerPixel + 7) / 8;
stride = bmp.PixelWidth * bytesPerPixel;
ellipseSize = stride * bmp.PixelHeight;
colorEllipse = new byte[ellipseSize];
// init gomme
ellipseRectBack = ellipseRect;
colorEllipseBack = new byte[ellipseSize];
bmp.CopyPixels(ellipseRect, colorEllipse, stride, 0);
}
// OPTION 2
private void CreatetBmpDrawingGroup()
{
DrawingGroup dwg = this.FindResource("dwg") as DrawingGroup;
DrawingVisual dv = new DrawingVisual();
using (DrawingContext ctx = dv.RenderOpen())
{
foreach (var item in dwg.Children)
{
ctx.DrawDrawing(item );
}
}
Rect bounds = dv.ContentBounds ;
RenderTargetBitmap bmp = new RenderTargetBitmap((int)bounds.Width, (int)bounds.Height, 96, 96, PixelFormats.Pbgra32);
bmp.Render(dv);
ellipseRect = new Int32Rect(0, 0, bmp.PixelWidth, bmp.PixelHeight);
bytesPerPixel = (bmp.Format.BitsPerPixel + 7) / 8;
stride = bmp.PixelWidth * bytesPerPixel;
ellipseSize = stride * bmp.PixelHeight;
colorEllipse = new byte[ellipseSize];
// init gomme
ellipseRectBack = ellipseRect;
colorEllipseBack = new byte[ellipseSize];
bmp.CopyPixels(ellipseRect, colorEllipse, stride, 0);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("button 1");
}
private void button2_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("button 2");
}
private void rectangle1_MouseDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("rectangle 1");
}
private void ellipse1_MouseDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("ellipse 1");
}
}
} |
Partager