WPF桌面端开发5-常用技巧

2020-05-09 14:56:55 浏览数 (1)

光标

自定义光标

代码语言:javascript复制
StreamResourceInfo sri = Application.GetResourceStream(new Uri(@"curerase.cur", UriKind.Relative));
m_canvas.Cursor = new Cursor(sri.Stream);

其中curerase.cur位于项目根目录下

使用系统光标

代码语言:javascript复制
m_canvas.Cursor = Cursors.Arrow;

Canvas

在做黑板的时候我们需要显示一个橡皮擦,它位于Canvas的最顶层

代码语言:javascript复制
Canvas.SetZIndex(m_erase_img, int.MaxValue);

获取显示器的缩放倍数

我们在开发截屏的功能时如果设置了缩放与布局为200%,显示分辨率为2560x1600,

我们通过代码SystemParameters.PrimaryScreenWidth获取的屏幕宽度就是1280,

如果截图截取1280的话,截出的图片就宽高都只有一半,

所以我们就必须获取系统缩放的倍数

代码语言:javascript复制
//100%的时候,DPI是96;这条语句的作用时获取缩放倍数
float factor = Graphics.FromHwnd(IntPtr.Zero).DpiX / 96;

Bitmap/BitmapImage/BitmapSource

BitmapSource是Imagesource的子类

WPF的Image控件中设置ImageSource

代码语言:javascript复制
image1.Source = new BitmapImage(new Uri(@"image file path", Urikind.RelativeOrAbsolute));

还可以使用:

代码语言:javascript复制
System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read);

byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length);
fs.Close(); fs.Dispose();
System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = ms;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.EndInit();
ms.Dispose();
image1.Source = bitmapImage;

还可以使用:

代码语言:javascript复制
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.UriSource = new Uri(szPath);//szPath为图片的全路径
bitmapImage.EndInit();
bitmapImage.Freeze();
image1.Source = bitmapImage;

Bitmap => BitmapImage

先将Bitmap储存成memorystream,然后指定给BitmapImage

代码语言:javascript复制
private BitmapImage BitmapToBitmapImage(System.Drawing.Bitmap bitmap)
{
    BitmapImage bitmapImage = new BitmapImage();
    using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
    {
        bitmap.Save(ms, bitmap.RawFormat);
        bitmapImage.BeginInit();
        bitmapImage.StreamSource = ms;
        bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
        bitmapImage.EndInit();
        bitmapImage.Freeze();
    }
    return bitmapImage;
}

image1.Source = BitmapToBitmapImage(bitmap);

Bitmap => BitmapSource

代码语言:javascript复制
BitmapSource bs = Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

BitmapSource => Bitmap

代码语言:javascript复制
BitmapSource m = (BitmapSource)image1.Source;

System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(m.PixelWidth, m.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); 

System.Drawing.Imaging.BitmapData data = bmp.LockBits(
new System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); 

m.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride); bmp.UnlockBits(data);

0 人点赞