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
|
public Bitmap GetBitmap(System.Windows.Media.Imaging.BitmapSource bitmapsource)
{
//convert image format
var src = new System.Windows.Media.Imaging.FormatConvertedBitmap();
src.BeginInit();
src.Source = bitmapsource;
src.DestinationFormat = System.Windows.Media.PixelFormats.Bgra32;
src.EndInit();
//copy to bitmap
Bitmap bitmap = new Bitmap(src.PixelWidth, src.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
var data = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
src.CopyPixels(System.Windows.Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride);
bitmap.UnlockBits(data);
return bitmap;
}
private void button2_Click(object sender, EventArgs e)
{
//var icon = new MahApps.Metro.IconPacks.PackIconFontAwesome();
//icon.Kind = MahApps.Metro.IconPacks.PackIconFontAwesomeKind.AccusoftBrands;
var icon = new MahApps.Metro.IconPacks.PackIconModern();
icon.Kind = MahApps.Metro.IconPacks.PackIconModernKind.CabinetFiles;
Geometry geo = Geometry.Parse(icon.Data);
GeometryDrawing gd = new GeometryDrawing();
gd.Geometry = geo;
gd.Brush = System.Windows.Media.Brushes.Yellow;//icon.BorderBrush;
gd.Pen = new System.Windows.Media.Pen(System.Windows.Media.Brushes.Red, 96);
DrawingImage geoImage = new DrawingImage(gd);
//geoImage.Freeze();
var img = new System.Windows.Controls.Image();
img.Source = geoImage;
var bitmap = new System.Windows.Media.Imaging.RenderTargetBitmap(256, 256, 32, 32, PixelFormats.Default);
bitmap.Render(img);
var bmp = GetBitmap(bitmap);
bmp.Save("e:\\tmp\\a.bmp", ImageFormat.Bmp);
SBmp = bmp;
panel1.Invalidate();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
if (SBmp!=null)
{
e.Graphics.DrawImage(SBmp, 0, 0);
}
} |
Partager