- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
以下代码截取屏幕的一部分(在鼠标坐标处)并将其显示在 Image 控件中。
public partial class MainWindow : Window
{
Timer timer = new Timer(100);
public MainWindow()
{
InitializeComponent();
timer.Elapsed += Timer_Elapsed;
timer.Start();
}
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
viewerImage.Source = GetSnapAtMouseCoords((int)((Grid)viewerImage.Parent).ActualWidth, (int)((Grid)viewerImage.Parent).ActualHeight, System.Windows.Forms.Cursor.Position);
}
private BitmapSource GetSnapAtMouseCoords(int width, int height, System.Drawing.Point mousePosition)
{
IntPtr handle = IntPtr.Zero;
try
{
using (var screenBmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
{
using (var bmpGraphics = Graphics.FromImage(screenBmp))
{
bmpGraphics.CopyFromScreen(mousePosition.X, mousePosition.Y, 0, 0, screenBmp.Size);
handle = screenBmp.GetHbitmap();
var bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
handle,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
return bs;
}
}
}
finally
{
DeleteObject(handle);
}
}
}
一切正常,直到我将图像源设置为 BitmapSource。不幸的是,图像永远不会在屏幕上呈现。
我认为这可能是因为我在 GUI 线程上创建了 BitmapSource...但我不太确定。
欢迎任何建议或想法。
最佳答案
实际上这是因为您在不同的线程上访问您的 GUI。您可以像这样包装初始调用:
Dispatcher.BeginInvoke(new Action(() =>
{
viewerImage.Source = GetSnapAtMouseCoords(
(int)((Grid)viewerImage.Parent).ActualWidth,
(int)((Grid)viewerImage.Parent).ActualHeight,
System.Windows.Forms.Cursor.Position);
}));
或者在后台线程中进行所有处理,只返回一个Frozen
(线程安全)BitmapSource
。您将悬停需要以不同方式传递 (int)((Grid)viewerImage.Parent).ActualWidth
,因为它也由 UI 线程拥有。
bs.Freeze();
Dispatcher.BeginInvoke(new Action(() =>
{
viewerImage.Source = bs;
}));
关于c# - 在 WPF 中显示 BitmapSource 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37437514/
如何在 C# 中高效地将 BitmapSource 转换为 byte[],反之亦然? 最佳答案 BitmapSource 到 byte[]: private byte[] BitmapSourceTo
我的目标是在 WPF 窗口上重写 OnRender 方法中绘制图像“someImage.png”,它是嵌入资源: protected override void OnRender(System.Win
我正在尝试将 native 图像数据结构编码到 BitmapSource。 这是原生结构: struct Bitmap_8Bit { public:
我有这个 BitmapSource source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetH
为 Metro Style 应用程序实现本地缓存的最佳模式是什么,以便可以在应用程序在线使用时在后台缓存图像并在应用程序离线时提供图像? 我们如何在发现无法访问互联网时将 BitmapSource 设
如何从图像文件加载 BitmapSource? 最佳答案 The BitmapImage class is a non-abstract subclass of BitmapSource ,因此只需使
我正在使用此 C# 代码访问图像文件,以便从中读取元数据。 BitmapSource img = BitmapFrame.Create(uri); 不幸的是,uri 指定的图像文件被锁定,直到程序结束
我如何创建一个 BitmapSource d,它是 BitmapSource s 旋转任意角度 a 的内容? RotateTransform 不适用,因为它仅限于 90 度倍数的角度。 编辑:Rota
我是 WPF 和 GDI 的新手,在显示图像时遇到问题。我的最终目标是构建类似暴露的东西。到目前为止,我将屏幕变灰,收集所有事件的 HWND,并捕获所有窗口的屏幕。现在,我有一个 Image 元素,我
我有相当多的图像要加载到我的 WPF 应用程序的列表框中。最初我使用 GDI 来调整图像大小(原件占用太多内存)。这很好,除了他们每张图像花费大约 400 毫秒。不太好。因此,在寻找另一种解决方案时,
我有一个 .NET BitmapSource目的。我想读取位图角上的四个像素,并测试它们是否都比白色暗。我该怎么做? 编辑:我不介意使用更好的 API 将此对象转换为另一种类型。 最佳答案 Bitma
我正在开发一个 BlackJack 程序,该程序显示 BlackJack 牌 table 、纸牌等。计划是它会使用自动化策略接连玩数千手牌。 我有一个 PlayerSeat UserControl,它
我将一个 Image.Source 属性绑定(bind)到下面显示的属性的结果。 public BitmapSource MyImageSource { get { B
您应该如何处理 BitmapSource ? // this wont work because BitmapSource doesnt implement IDisposable using(Bit
我需要将 Clipboard.GetImage() 的内容(一个 BitmapSource)解析为一个 BitmapImage。有谁知道如何做到这一点? 最佳答案 我找到了一个干净的解决方案: Bit
我是 WPF 的初学者,正在尝试一个家庭项目以熟悉该技术。我有一个简单的表单,用户可以在其中选择一个图像文件,然后显示 EXIF 数据和图像的缩略图。这工作正常,但是当我选择一个 RAW 图像文件(~
我创建了一个 WPF 控件(从 FrameworkElement 继承),它显示可以平移的平铺图形。每个图块为 256x256 像素,24bpp。我已经覆盖了 OnRender。在那里,我加载任何新图
如何将 BitmapSource 转换为 MemoryStream。虽然我尝试了一些代码: private Stream StreamFromBitmapSource(BitmapSource wri
系统:Windows XP SP3,.NET 3.5,4GB 内存,双 1.6GHz 我有一个 WPF 应用程序,可以加载和转换(使用 Storyboard动画)非常大的 PNG。这些 PNG 的分辨
在 WPF 中, System.Windows.Clipboard.getImage() 函数返回 BitmapSource 目的。作为来自 WinForms 背景的 WPF 新手,我不清楚如何将此图
我是一名优秀的程序员,十分优秀!