首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

在Metro Windows 8中将ImageSource转换为WriteableBitmap

在Metro Windows 8中,将ImageSource转换为WriteableBitmap可以通过以下步骤实现:

  1. 首先,需要在项目中引入Windows.UI.Xaml.Media.Imaging命名空间。
  2. 然后,可以使用ImageSourceConverter类将ImageSource转换为WriteableBitmap。

以下是一个示例代码:

代码语言:csharp
复制
// 将ImageSource转换为WriteableBitmap
private async Task<WriteableBitmap> ConvertImageSourceToWriteableBitmap(ImageSource imageSource)
{
    WriteableBitmap writeableBitmap = null;
    if (imageSource != null)
    {
        var stream = new InMemoryRandomAccessStream();
        var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
        var pixelData = await imageSource.ToPixelDataAsync();
        encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)imageSource.PixelWidth, (uint)imageSource.PixelHeight, 96.0, 96.0, pixelData);
        await encoder.FlushAsync();
        stream.Seek(0);
        var bitmapImage = new BitmapImage();
        bitmapImage.SetSource(stream);
        writeableBitmap = new WriteableBitmap(bitmapImage.PixelWidth, bitmapImage.PixelHeight);
        stream.Seek(0);
        await writeableBitmap.SetSourceAsync(stream);
    }
    return writeableBitmap;
}

在这个示例代码中,我们首先创建了一个InMemoryRandomAccessStream对象,然后使用BitmapEncoder将ImageSource转换为PNG格式的数据,并将其写入到InMemoryRandomAccessStream对象中。接着,我们使用BitmapImage对象将InMemoryRandomAccessStream对象中的数据读取出来,并将其设置为WriteableBitmap对象的源。最后,我们返回WriteableBitmap对象。

需要注意的是,这个示例代码中的ToPixelDataAsync()方法是扩展方法,需要自己实现。具体实现可以参考以下代码:

代码语言:csharp
复制
public static class ImageSourceExtensions
{
    public static async Task<byte[]> ToPixelDataAsync(this ImageSource imageSource)
    {
        byte[] pixelData = null;
        if (imageSource != null)
        {
            var randomAccessStream = new InMemoryRandomAccessStream();
            var bitmapEncoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, randomAccessStream);
            var softwareBitmap = await imageSource.ToSoftwareBitmapAsync();
            bitmapEncoder.SetSoftwareBitmap(softwareBitmap);
            await bitmapEncoder.FlushAsync();
            pixelData = (await randomAccessStream.ToArrayAsync()).ToArray();
        }
        return pixelData;
    }

    private static async Task<SoftwareBitmap> ToSoftwareBitmapAsync(this ImageSource imageSource)
    {
        SoftwareBitmap softwareBitmap = null;
        if (imageSource is SoftwareBitmapSource softwareBitmapSource)
        {
            softwareBitmap = softwareBitmapSource.SoftwareBitmap;
        }
        else if (imageSource is WriteableBitmap writeableBitmap)
        {
            softwareBitmap = SoftwareBitmap.CreateCopyFromBuffer(writeableBitmap.PixelBuffer, BitmapPixelFormat.Bgra8, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight);
        }
        else if (imageSource is BitmapImage bitmapImage)
        {
            softwareBitmap = await bitmapImage.ToSoftwareBitmapAsync();
        }
        return softwareBitmap;
    }

    private static async Task<SoftwareBitmap> ToSoftwareBitmapAsync(this BitmapImage bitmapImage)
    {
        SoftwareBitmap softwareBitmap = null;
        if (bitmapImage != null)
        {
            var randomAccessStream = new InMemoryRandomAccessStream();
            var bitmapEncoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, randomAccessStream);
            bitmapEncoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)bitmapImage.PixelWidth, (uint)bitmapImage.PixelHeight, 96.0, 96.0, await bitmapImage.ToPixelDataAsync());
            await bitmapEncoder.FlushAsync();
            randomAccessStream.Seek(0);
            softwareBitmap = await BitmapDecoder.CreateAsync(randomAccessStream).AsTask().ContinueWith(async (task) =>
            {
                var bitmapDecoder = task.Result;
                return await bitmapDecoder.GetSoftwareBitmapAsync();
            }).Unwrap().ConfigureAwait(false);
        }
        return softwareBitmap;
    }

    private static async Task<byte[]> ToArrayAsync(this IRandomAccessStream randomAccessStream)
    {
        byte[] data = new byte[randomAccessStream.Size];
        await randomAccessStream.ReadAsync(data.AsBuffer(), (uint)randomAccessStream.Size, InputStreamOptions.None);
        return data;
    }
}

这个扩展方法包含了ImageSource对象到SoftwareBitmap对象的转换,以及SoftwareBitmap对象到字节数组的转换。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

win10 uwp 读取保存WriteableBitmap 、BitmapImage 保存 WriteableBitmap 到文件从文件读 WriteableBitmapIma

我们UWP,经常使用的图片,数据结构就是 BitmapImage 和 WriteableBitmap。关于 BitmapImage 和 WriteableBitmap 区别,我就不在这里说。...byte[] 如果我们的 ImageSource 是 BitmapImage ,那么我们不能使用上面的办法,直接保存 WriteableBitmap ,我们可以使用截图 private async...是 WriteableBitmap ,直接保存 我们使用 byte[] 传输时不好,不能用在 http 传输上(不是一定的不能),所以我们就把它转为base64,我提供了很多方法把数组 base64...WriteableBitmap 我使用http://www.cnblogs.com/cjw1115/p/5164327.html 大神的,直接WriteableBitmap bitmap = imageSource...UWP的 BitmapImage 不能转换为 byte[] 或 WriteableBitmap 。这句话是错的。

1.9K10

使用不安全代码将 Bitmap 位图转为 WPF 的 ImageSource 以获得高性能和持续小的内存占用

更新于 2017-11-10 06:42 WPF 中将一个现成的 Bitmap 位图转换成 ImageSource 用于显示一个麻烦的事儿...---- WPF 官方提供了一种方法,使用 System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap() 方法。...---- 持续输出图像的时候(例如播放 Gif 图、持续显示屏幕截图等)不及时释放内存非常致命!为了防止重复创建图片,WriteableBitmap 似乎成了比较好的选择。...var width = ws; var height = hs; var bytes = ws * hs * wb.Format.BitsPerPixel / 8;...我的朋友林德熙为此将这段代码简化得只剩下几行代码了:WPF 使用不安全代码快速从数组 WriteableBitmap - 林德熙。

1K20

WPF 如何在 WriteableBitmap 写文字

最近看到WPF 使用不安全代码快速从数组 WriteableBitmap 可以快速从数组 WriteableBitmap 所以就让他画一些元素,但是发现元素有文字就没法了。...截图 这个方法是从 WriteableBitmapEx看到的,可以页面创建一个 TextBlock 让他来显示文字,然后使用截图获得文字,把图片画到 WriteableBitmap 就好。...image, bmp); 上面的代码可能无法直接运行,于是我就给 CopyFrom 代码,代码实际是从WPF 使用不安全代码快速从数组 WriteableBitmap - 林德熙 复制 public...width = ws; var height = hs; var bytes = ws * hs * wb.Format.BitsPerPixel / 8;...SharpGL ,感觉还不错,如果需要比较高的速度,那么推荐使用这个库 SharpGL(Opengl)入门之纹理星球 - BIT祝威 - 博客园 使用不安全代码将 Bitmap 位图转为 WPF 的 ImageSource

45010

WPF 如何在 WriteableBitmap 写文字 截图win form 方法

最近看到WPF 使用不安全代码快速从数组 WriteableBitmap 可以快速从数组 WriteableBitmap 所以就让他画一些元素,但是发现元素有文字就没法了。...截图 这个方法是从 WriteableBitmapEx看到的,可以页面创建一个 TextBlock 让他来显示文字,然后使用截图获得文字,把图片画到 WriteableBitmap 就好。...image, bmp); 上面的代码可能无法直接运行,于是我就给 CopyFrom 代码,代码实际是从WPF 使用不安全代码快速从数组 WriteableBitmap - 林德熙 复制 public...width = ws; var height = hs; var bytes = ws * hs * wb.Format.BitsPerPixel / 8;...SharpGL ,感觉还不错,如果需要比较高的速度,那么推荐使用这个库 SharpGL(Opengl)入门之纹理星球 - BIT祝威 - 博客园 使用不安全代码将 Bitmap 位图转为 WPF 的 ImageSource

63120

WPF 使用不安全代码快速从数组 WriteableBitmap

使用不安全代码转换是把数组直接复制到WriteableBitmap,请看使用不安全代码将 Bitmap 位图转为 WPF 的 ImageSource 以获得高性能和持续小的内存占用 - walterlv...,这里讲了如何从 Bitmap WriteableBitmap ,于是下面只需要把数组 Bitmap 就可以了。...我最后会给大家全部代码,所以现在讲原理。 如果已经拿到了数组,知道数组的存放,那么就可以直接把数组复制到 WriteableBitmap 就可以显示。...这就是PixelFormat指定的类型,可以使用Bgra32或者其他的格式,不过指定了格式就需要数组存放和指定一样 因为没有直接从数组 WriteableBitmap 所以需要先把数组 Bitmap...E4%B8%8D%E5%AE%89%E5%85%A8%E4%BB%A3%E7%A0%81%E5%BF%AB%E9%80%9F%E4%BB%8E%E6%95%B0%E7%BB%84%E8%BD%AC-WriteableBitmap.html

91210

win10 uwp 如何创建修改保存位图 创建保存图片在 Image 控件使用WriteableBitmap 转换通过读写像素转换 CanvasBitmap

实际上 Softwarebitmap 和 WriteableBitmap 是差不多的。但是 Softwarebitmap 可以支持 WriteableBitmap 、 Direct3D 和代码修改。...= new Windows.Graphics.Imaging.BitmapPropertySet(); var qualityValue = new Windows.Graphics.Imaging.BitmapTypedValue...BGRA8 格式而且需要先计算透明值,转换打开 SoftwareBitmap 静态函数 Convert 让格式 Image 控件支持。...实际上通过 下面代码可以把 SoftwareBitmap ImageBrush 显示 var imageBrush = new ImageBrush {ImageSource...BitmapPixelFormat.Bgra8, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight ); 通过读写像素 是不是看到上面的教程感觉这个博客很简单

2.2K10

dotnet 读 WPF 源代码笔记 了解 WPF 已知问题 后台线程创建 WriteableBitmap 锁住主线程

WPF 中,如果在没有开启 Dispatcher 的后台线程里面创建 WriteableBitmap 对象, WriteableBitmap 构造函数传入主线程创建的 BitmapSource...然而后台线程后续需要等待主线程返回,才能完成创建图片,因此主线程等待后台线程的锁而后台线程等待主线程返回,两个线程等待 通过 WPF 仓库的源代码可以看到 WriteableBitmap.InitFromBitmapSource...: ImageSource, DUCE.IResource { internal virtual void UpdateBitmapSourceResource(DUCE.Channel...System.Windows.Media.Imaging.WriteableBitmap.WriteableBitmap(System.Windows.Media.Imaging.BitmapSource...97%AE%E9%A2%98-%E5%90%8E%E5%8F%B0%E7%BA%BF%E7%A8%8B%E5%88%9B%E5%BB%BA-WriteableBitmap-%E9%94%81%E4%BD

53320

《101 Windows Phone 7 Apps》读书笔记-BABY MILESTONES

Windows Phone应用程序中,list box最常用的SelectionChanged事件(只有选定的内容改变以后才会触发,而非点击操作就可以)在这里是不希望出现的。...与图片相关的部分在LoadFile中,它调用PictureDecoder.DecodeJpeg(Microsoft.Phone命名空间中)将流转换为ImageSource,从而可以将其设置为Image...➔ DecodeJpeg方法的速度相当慢,并且它必须在UI线程中调用,所以,这个类会缓存所有它创建的ImageSource,使得下次其文件名被传递给LoadFile时,能够快速返回(相同的ImageSource...除了PictureDecoder.DecodeJpeg,可以考虑使用WriteableBitmap.LoadJpeg。后者可以在后台线程中调用,避免解码一个大的图片时所带来的响应迟滞。...WriteableBitmap会在第42章的“Jigsaw Puzzle”中进行介绍。     LoadFile可以使用一个替代的方法来使用隔离存储空间中的图片构造一个ImageSource

782100

WPF 如何在 WriteableBitmap 写文字

最近看到WPF 使用不安全代码快速从数组 WriteableBitmap 可以快速从数组 WriteableBitmap 所以就让他画一些元素,但是发现元素有文字就没法了。...截图 这个方法是从 WriteableBitmapEx看到的,可以页面创建一个 TextBlock 让他来显示文字,然后使用截图获得文字,把图片画到 WriteableBitmap 就好。...ActualWidth, (int)ActualHeight)); wb.Unlock(); win form 方法 另一个方法是使用 win form 写文字然后使用 WPF 使用不安全代码快速从数组...image, bmp); 上面的代码可能无法直接运行,于是我就给 CopyFrom 代码,代码实际是从WPF 使用不安全代码快速从数组 WriteableBitmap - 林德熙 复制 public...width = ws; var height = hs; var bytes = ws * hs * wb.Format.BitsPerPixel / 8;

1.8K10

Win8快捷键

作为微软革命性的产品,Windows8带来全新的体验,尤其是Metro界面的引入,让Win8完全不是Windows的模样了。...操作习惯上,Metro界面更多的照顾平板电脑等触摸设备,针对传统的键盘鼠标操作,Win8引导着用户尽可能多的使用快捷键,此外还要学会善用鼠标右键。...微软的这些操作上的改进,也是照顾着非触摸屏设备的用户,Metro界面,为平板而生,但是绝不会抛弃PC。...标键 + V 屏幕上的通知中循环切换 Windows 键 + Page Up 多监视器设置中将开始屏幕移动至左监视器 Windows 键 + Page Down 多监视器设置中将开始屏幕移动至右监视器...当您将应用程序向一侧对齐时,此热键会将拆分栏移动至左侧 原创文章采用CC BY-NC-SA 4.0协议进行许可,转载请注明转载自:Win8快捷键

1.3K30

dotnet 读 WPF 源代码笔记 WriteableBitmap 的渲染和更新是如何实现

WPF 框架提供方便进行像素读写的 WriteableBitmap 类,本文来告诉大家咱写下像素到 WriteableBitmap 渲染,底层的逻辑 之前我使用 WriteableBitmap 进行...CPU 高性能绘图时,性能调试遇到一个问题,写入到 WriteableBitmap 的像素会经过两次拷贝。...如何在 WriteableBitmap 写文字 WPF 使用不安全代码快速从数组 WriteableBitmap WriteableBitmap 进行绘制时,有一个重要的功能是设置 DirtyRect...收集过程中将会调用到 CSwDoubleBufferedBitmap 的 CopyForwardDirtyRects 方法,这个方法的作用就是根据脏区从后面的缓存将像素复制到前面的缓存。...%A3%E7%A0%81%E7%AC%94%E8%AE%B0-WriteableBitmap-%E7%9A%84%E6%B8%B2%E6%9F%93%E5%92%8C%E6%9B%B4%E6%96%B0%

84620

dotnet WPF 里显示数学 π 的颜色

于是我就来写一个逗比的应用将 π 的颜色 WPF 应用画出来。...原理就是读取 π 的小数点后的数值,然后使用逗比算法转换为 RGB 颜色像素,接着将这些像素转换为一张图片 以下就是我用程序生成的 π 图片 我先从某个有趣的地方随便找到了 π 小数点之后很长的数值,...writeableBitmap.Format.BitsPerPixel / 8; for (int i = 0, j = 0; i + 4 < length && j + 3 <...,欢迎大家乱写 本文所有代码放在 github 和 gitee 欢迎访问 可以通过如下方式获取本文的源代码,先创建一个空文件夹,接着使用命令行 cd 命令进入此空文件夹,命令行里面输入以下代码,即可获取到本文的代码...WPF-%E9%87%8C%E6%98%BE%E7%A4%BA%E6%95%B0%E5%AD%A6-%CF%80-%E7%9A%84%E9%A2%9C%E8%89%B2.html ,以避免陈旧错误知识的误导

74810
领券