我有一个a的实例,System.Drawing.Bitmap并希望以一种形式将它提供给我的WPF应用程序System.Windows.Media.Imaging.BitmapImage.
对此最好的方法是什么?
我需要逐个像素地绘制图像并将其显示在WPF中.我试图通过使用System.Drawing.Bitmap然后使用CreateBitmapSourceFromHBitmap()创建BitmapSource一个WPF图像控件来做到这一点.我在某处有内存泄漏,因为当CreateBitmapSourceFromBitmap()重复调用时,内存使用率会上升,并且在应用程序结束之前不会下降.如果我不打电话CreateBitmapSourceFromBitmap(),内存使用量没有明显变化.
for (int i = 0; i < 100; i++)
{
var bmp = new System.Drawing.Bitmap(1000, 1000);
var source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
source = null;
bmp.Dispose();
bmp = null;
}
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能释放BitmapSource记忆?
据我所知,从BitmapSource转换为Bitmap的唯一方法是通过不安全的代码...像这样(来自Lesters WPF博客):
myBitmapSource.CopyPixels(bits, stride, 0);
unsafe
{
fixed (byte* pBits = bits)
{
IntPtr ptr = new IntPtr(pBits);
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(
width,
height,
stride,
System.Drawing.Imaging.PixelFormat.Format32bppPArgb,ptr);
return bitmap;
}
}
Run Code Online (Sandbox Code Playgroud)
做反过来:
System.Windows.Media.Imaging.BitmapSource bitmapSource =
System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
bitmap.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
Run Code Online (Sandbox Code Playgroud)
框架中有更简单的方法吗?它不在那里的原因是什么(如果不是)?我认为它相当实用.
我需要它的原因是因为我使用AForge在WPF应用程序中执行某些图像操作.WPF希望显示BitmapSource/ImageSource,但AForge可以在Bitmaps上运行.
我有一个System.Drawing.Image的实例.
如何在我的WPF应用程序中显示这个?
我尝试过,img.Source但这不起作用.
我需要在WPF控件上显示实时图像.我正在寻找使用WPF执行此操作的最快方法.
我正在使用其DLL API(AVT)从相机捕获图像.
图像由dll写入,摄像机使用IntPtr将回调上升到名为tFrame的Image结构(如下所述).像素数据存储在ImageBuffer属性中,其中InPtr为字节数组.
我知道如何从像素字节数组创建一个位图,但不是BitmapImage.因此可以创建一个Bitmap,然后从中创建一个BitmapImagem. 这里有一种从内存上的位图创建BitmapImage的方法.但我想直接从数据源(tFrame)创建BitmapImage.我怎样才能做到这一点?
我知道BitmapImage有一个CopyPixels方法,但它有一个SetPixels.
public struct tFrame
{
public IntPtr AncillaryBuffer;
public uint AncillaryBufferSize;
public uint AncillarySize;
public tBayerPattern BayerPattern;
public uint BitDepth;
public tFrameCtx Context;
public tImageFormat Format;
public uint FrameCount;
public uint Height;
public IntPtr ImageBuffer;
public uint ImageBufferSize;
public uint ImageSize;
public uint RegionX;
public uint RegionY;
public tErr Status;
public uint TimestampHi;
public uint TimestampLo;
public uint Width;
}
Run Code Online (Sandbox Code Playgroud)
这是我如何从像素字节数组创建位图.这是在WinForm版本的软件中使用的.
private void CreateBitmap(tFrame frame)
{
//This …Run Code Online (Sandbox Code Playgroud) 我正在遵循将a绑定MenuItem到数据对象的示例.
<Menu Grid.Row="0" KeyboardNavigation.TabNavigation="Cycle"
ItemsSource="{Binding Path=MenuCommands}">
<Menu.ItemContainerStyle>
<Style>
<Setter Property="MenuItem.Header" Value="{Binding Path=DisplayName}"/>
<Setter Property="MenuItem.ItemsSource" Value="{Binding Path=Commands}"/>
<Setter Property="MenuItem.Command" Value="{Binding Path=Command}"/>
<Setter Property="MenuItem.Icon" Value="{Binding Path=Icon}"/>
</Style>
</Menu.ItemContainerStyle>
</Menu>
Run Code Online (Sandbox Code Playgroud)
这一切都在游泳,除了MenuItem图标显示为字符串System.Drawing.Bitmap.有问题的位图由数据对象从已编译的资源返回.
internal static System.Drawing.Bitmap folder_page
{
get
{
object obj = ResourceManager.GetObject("folder_page", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我想把我BitmapImage变成一个System.Drawing.Image?
这是我的BitmapImage:
BitmapImage bmi = new BitmapImage(new Uri(someString, UriKind.Absolute));
Run Code Online (Sandbox Code Playgroud)
现在,我将如何创建一个System.Drawing.Image来自我的bmi?
如何从字节数组创建System.Windows.Media.ImageSource?
我有一个字节数组,包含TIFF图像文件的完整和完整的文件内容.我需要在屏幕上显示这个,我不知道从哪里开始.
据说,它可以完成(根据我的老板,我们的开发团队过去曾做过,但没有人记得如何).
以前有没有人做过这样的事情?
我正在与Winforms中的Emgu Cv一起使用Kinect进行人脸识别.现在,我想转向WPF.但是,EmguCv库仅支持Bitmap类.
我可以在WPF中使用Bitmap类(在Winforms中使用)吗?如果没有,是否有其他方法在WPF中使用带有kinect的Emgu cv?
谢谢.
如何在Windows 8应用程序的WINRT XAML C#代码中为文本框添加水印.有一个工具提示的直接属性,但同样不适用于Watermark,那么实现这一目的的最短有效方法是什么?
wpf ×9
c# ×7
bitmap ×3
.net ×2
bitmapimage ×2
image ×2
.net-3.5 ×1
bitmapsource ×1
bytearray ×1
c#-4.0 ×1
data-binding ×1
emgucv ×1
icons ×1
imagesource ×1
kinect ×1
memory-leaks ×1
menuitem ×1
mvvm ×1
tiff ×1
windows-8 ×1
xaml ×1