如何将字符串转换为位图图像

本文关键字:位图 图像 转换 字符串 | 更新日期: 2023-09-27 18:24:48

请告诉我如何在c#中的wpf中转换位图图像中的文本或字符串。因为没有像windows窗体那样可用的位图类。。感谢Advance:)

如何将字符串转换为位图图像

BitmapImage就是您想要的。请参阅MSDN链接中提供的示例。

// Create the image element.
Image simpleImage = new Image();    
simpleImage.Width = 200;
simpleImage.Margin = new Thickness(5);
// Create source.
BitmapImage bi = new BitmapImage();
// BitmapImage.UriSource must be in a BeginInit/EndInit block.
bi.BeginInit();
bi.UriSource = new Uri(@"/sampleImages/cherries_larger.jpg",UriKind.RelativeOrAbsolute);
bi.EndInit();
// Set the image source.
simpleImage.Source = bi;

此外,您可以直接在XAML中设置源,WPF通过字符串到ImageSource的默认转换器将其内部转换为ImageSource。

<Image Source="/sampleImages/cherries_larger.jpg"/>