110,533
社区成员
发帖
与我相关
我的任务
分享
protected override void OnRender(DrawingContext dc)
{
base.OnRender(dc);
Random rd = new Random();
System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Red);
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap((int)SystemParameters.WorkArea.Width, (int)SystemParameters.WorkArea.Height);
using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap))
{
for (int i = 0; i < 20000; i++)
{
int x = rd.Next(Convert.ToInt32(ActualWidth));
int y = rd.Next(Convert.ToInt32(ActualHeight));
System.Drawing.Rectangle r = new System.Drawing.Rectangle(x, y, 10, 10);
graphics.DrawRectangle(pen, r);
}
graphics.Dispose();
}
BitmapImage bitmapImage = new BitmapImage();
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
bitmap.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp);
bitmapImage.BeginInit();
bitmapImage.StreamSource = ms;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
bitmapImage.Freeze();
}
dc.DrawImage(bitmapImage, new Rect(0, 0, bitmap.Width, bitmap.Height));
}