前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WPF桌面端开发5-常用技巧

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

作者头像
码客说
发布2020-05-09 14:56:55
8270
发布2020-05-09 14:56:55
举报
文章被收录于专栏:码客码客码客

光标

自定义光标

StreamResourceInfo sri = Application.GetResourceStream(new Uri(@"cur\erase.cur", UriKind.Relative));
m_canvas.Cursor = new Cursor(sri.Stream);

其中cur\erase.cur位于项目根目录下

使用系统光标

m_canvas.Cursor = Cursors.Arrow;

Canvas

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

Canvas.SetZIndex(m_erase_img, int.MaxValue);

获取显示器的缩放倍数

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

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

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

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

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

Bitmap/BitmapImage/BitmapSource

BitmapSource是Imagesource的子类

WPF的Image控件中设置ImageSource

image1.Source = new BitmapImage(new Uri(@"image file path", Urikind.RelativeOrAbsolute));

还可以使用:

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;

还可以使用:

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

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

BitmapSource bs = Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

BitmapSource => Bitmap

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);
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-01-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 光标
  • Canvas
  • 获取显示器的缩放倍数
  • Bitmap/BitmapImage/BitmapSource
    • WPF的Image控件中设置ImageSource
      • Bitmap => BitmapImage
        • Bitmap => BitmapSource
          • BitmapSource => Bitmap
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档