将System.Windows.Controls.Image加载到WritableBitmap对象

wsc

我已经在xaml代码中创建了Image对象:

<Image Name="Square" Source="Square.png" Height="400" Width="640"/>

在后面的代码中,我写了:

WriteableBitmap bitmap = new WriteableBitmap((int)Square.Width, (int)Square.Height, 96, 96, PixelFormats.Rgb24, null);

如果我要创建像素数组,则将其放在WritableBitmap中,然后放入Image中,如下所示:

    int width = 300;
    int height = 300;
    WriteableBitmap bitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Rgb24, null);
    uint[] pixels = new uint[width * height];

    bitmap.WritePixels(new Int32Rect(0, 0, 300, 300), pixels, width * 4, 0);
    **Square.Source = bitmap;**

我没有任何问题,但是当我尝试将在xaml代码中创建的图像加载到WritableBitmap时,我无法执行以下操作:

WriteableBitmap bitmap = new WriteableBitmap((int)Square.Width, (int)Square.Height, 96, 96, PixelFormats.Rgb24, null);
bitmap = Square.Source; // Cannot implicitly convert type 'System.Windows.Media.ImageSource' to 'System.Windows.Media.Imaging.WriteableBitmap'

所以,如果我不能转换型“System.Windows.Media.ImageSource”“System.Windows.Media.Imaging.WriteableBitmap”,为什么我可以把“System.Windows.Media.Imaging.WriteableBitmap”“System.Windows .Media.ImageSource'

    Square.Source = bitmap;
    bitmap = Square.Source; // Why it does not work?

谢谢你的帮助。

克莱门斯

您不能直接WriteableBitmap从any创建一个ImageSource,因为mayImageSource可能不是a BitmapSource(Writeablebitmap构造函数将其作为参数)。

但是,根据您的情况,可以将该Source属性强制转换为BitmapSource

var bitmap = new WriteableBitmap((BitmapSource)Square.Source);

请注意,SourceImage控件属性属于ImageSource(而不是BitmapSource类型因为该控件不仅可以显示位图(例如矢量图像),还可以显示其他类型的图像。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

'无法将类型为'System.String'的对象转换为类型'System.Windows.Controls.ComboBoxItem

来自分类Dev

将System.Drawing.Icon转换为System.Windows.Controls.Image

来自分类Dev

无法将类型为“ System.Windows.Controls.Grid”的对象转换为类型为“ System.Windows.Shapes.Ellipse”的对象?

来自分类Dev

将 System.Windows.Controls.Image 与多个图像一起使用时出现内存问题

来自分类Dev

'System.Windows.Controls.Image'不包含'FromFile'的定义

来自分类Dev

将System.Windows.Forms.WebBrowser添加到System.Windows.Controls.StackPanel无法转换为System.Windows.UIElement

来自分类Dev

使用继承XAML内部的System.Windows.Controls.Control的类

来自分类Dev

更改类system.windows.controls.menuitem中的标题/内容?

来自分类Dev

错误:System.Windows.Controls.UIElementCollection'不包含'OfType'的定义

来自分类Dev

防止存储“System.Windows.Controls.Label: value”

来自分类Dev

无法将数据网格的选定行发送到数据库,但发送 System.Windows.Controls.TextBlock 的文本

来自分类Dev

如何在System.Windows.Forms.Form中添加System.Windows.Controls.WebBrowser

来自分类Dev

如何在System.Windows.Controls.TextBox C#中设置位置或类似属性

来自分类Dev

在“ System.Windows.Controls.Button”的名称范围中找不到名称

来自分类Dev

解决Set属性'System.Windows.Controls.Decorator.Child'在DataTemplate中引发异常

来自分类Dev

如何在不显示System.Windows.Controls.WebBrowser的情况下使用它?

来自分类Dev

如何在System.Windows.Controls.ListView中设置当前关注的项目?

来自分类Dev

C# WPF System.Windows.Controls.TreeView 如何更改选定节点

来自分类Dev

多选列表框项返回 system.windows.controls.listboxitem: 与 listboxitem 值

来自分类Dev

列表框在文件名之前显示“System.Windows.Controls.listboxitem”

来自分类Dev

如何通过jQuery将URL加载到Windows中?

来自分类Dev

将RPT文件加载到C#Windows服务中

来自分类Dev

Windows:将Haskell源代码加载到ghci中

来自分类Dev

通过Windows Form C#将库加载到Excel

来自分类Dev

将首选应用程序加载到Windows的RAM中

来自分类Dev

将纹理加载到对象上时的PyOpenGL和PIL.Image分色效果

来自分类Dev

无法将字符串转换为Windows.UI.Xaml.Controls.IconElement

来自分类Dev

无法将字符串转换为Windows.UI.Xaml.Controls.IconElement

来自分类Dev

将matplotlib对象加载到reportlab

Related 相关文章

  1. 1

    '无法将类型为'System.String'的对象转换为类型'System.Windows.Controls.ComboBoxItem

  2. 2

    将System.Drawing.Icon转换为System.Windows.Controls.Image

  3. 3

    无法将类型为“ System.Windows.Controls.Grid”的对象转换为类型为“ System.Windows.Shapes.Ellipse”的对象?

  4. 4

    将 System.Windows.Controls.Image 与多个图像一起使用时出现内存问题

  5. 5

    'System.Windows.Controls.Image'不包含'FromFile'的定义

  6. 6

    将System.Windows.Forms.WebBrowser添加到System.Windows.Controls.StackPanel无法转换为System.Windows.UIElement

  7. 7

    使用继承XAML内部的System.Windows.Controls.Control的类

  8. 8

    更改类system.windows.controls.menuitem中的标题/内容?

  9. 9

    错误:System.Windows.Controls.UIElementCollection'不包含'OfType'的定义

  10. 10

    防止存储“System.Windows.Controls.Label: value”

  11. 11

    无法将数据网格的选定行发送到数据库,但发送 System.Windows.Controls.TextBlock 的文本

  12. 12

    如何在System.Windows.Forms.Form中添加System.Windows.Controls.WebBrowser

  13. 13

    如何在System.Windows.Controls.TextBox C#中设置位置或类似属性

  14. 14

    在“ System.Windows.Controls.Button”的名称范围中找不到名称

  15. 15

    解决Set属性'System.Windows.Controls.Decorator.Child'在DataTemplate中引发异常

  16. 16

    如何在不显示System.Windows.Controls.WebBrowser的情况下使用它?

  17. 17

    如何在System.Windows.Controls.ListView中设置当前关注的项目?

  18. 18

    C# WPF System.Windows.Controls.TreeView 如何更改选定节点

  19. 19

    多选列表框项返回 system.windows.controls.listboxitem: 与 listboxitem 值

  20. 20

    列表框在文件名之前显示“System.Windows.Controls.listboxitem”

  21. 21

    如何通过jQuery将URL加载到Windows中?

  22. 22

    将RPT文件加载到C#Windows服务中

  23. 23

    Windows:将Haskell源代码加载到ghci中

  24. 24

    通过Windows Form C#将库加载到Excel

  25. 25

    将首选应用程序加载到Windows的RAM中

  26. 26

    将纹理加载到对象上时的PyOpenGL和PIL.Image分色效果

  27. 27

    无法将字符串转换为Windows.UI.Xaml.Controls.IconElement

  28. 28

    无法将字符串转换为Windows.UI.Xaml.Controls.IconElement

  29. 29

    将matplotlib对象加载到reportlab

热门标签

归档