PROBLEM:
Do you know something in .NET that can convert my text(string) and return an image type? Like .Net "Convert" Class does but it doesnt support image conversion. I mean.. just like passing a file information (filename and Path) as parameters and return image(Bitmap) to display. Do I really have to code this manually?
你知道.NET中可以转换我的文本(字符串)并返回图像类型的东西吗?像.Net“转换”类一样,但它不支持图像转换。我的意思是..就像传递文件信息(文件名和路径)作为参数并返回图像(Bitmap)来显示。我真的需要手动编码吗?
SCENARIO:
I successfully collect some list of image files in a directory(From Flash Drive or in local drive) and want to display those as an actual image.
我成功地在目录(从闪存驱动器或本地驱动器)中收集了一些图像文件列表,并希望将它们显示为实际图像。
Hope my problem is clear.
希望我的问题很清楚。
3 个解决方案
#1
0
Depending to whether you're using WinForms or WPF, you could use System.Drawing.Image
or System.Windows.Media.ImageSource
.
根据您使用的是WinForms还是WPF,您可以使用System.Drawing.Image或System.Windows.Media.ImageSource。
You can't convert a string to an on-screen image. You HAVE to load it in another component. For example, Image
has a static FromFile(string filepath)
method that loads the image and makes it available for display.
您无法将字符串转换为屏幕上的图像。您必须将其加载到另一个组件中。例如,Image有一个静态FromFile(字符串文件路径)方法,用于加载图像并使其可用于显示。
#2
0
Implement the IValueConverter interface , and return the bitmap.
实现IValueConverter接口,并返回位图。
public class MyValueConverter : IValueConverter {
/*
Implement this method to modify the source data before sending it to display
*/
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
try {
return new BitmapImage(new Uri("../Images/" + (string)(value), UriKind.Relative));
}
catch{
return new BitmapImage();
}
}
/*
Implement this method to modify the target data before sending it back to the source.
*/
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
var img = value as BitmapImage;
return img.UriSource.AbsoluteUri;
}
}
#3
0
You can use Bitmap.FromFile("FileName")
or you can use Image.FromFile("FileName")
.If you want to get an array of Bitmaps you can use a simple linq query
您可以使用Bitmap.FromFile(“FileName”)或者您可以使用Image.FromFile(“FileName”)。如果您想获得一个位图数组,您可以使用简单的linq查询
var fileNames = new string[] { "file1", "file2", "file3" };
var myImages = fileNames.Select(x => Bitmap.FromFile(x)).ToArray();
#1
0
Depending to whether you're using WinForms or WPF, you could use System.Drawing.Image
or System.Windows.Media.ImageSource
.
根据您使用的是WinForms还是WPF,您可以使用System.Drawing.Image或System.Windows.Media.ImageSource。
You can't convert a string to an on-screen image. You HAVE to load it in another component. For example, Image
has a static FromFile(string filepath)
method that loads the image and makes it available for display.
您无法将字符串转换为屏幕上的图像。您必须将其加载到另一个组件中。例如,Image有一个静态FromFile(字符串文件路径)方法,用于加载图像并使其可用于显示。
#2
0
Implement the IValueConverter interface , and return the bitmap.
实现IValueConverter接口,并返回位图。
public class MyValueConverter : IValueConverter {
/*
Implement this method to modify the source data before sending it to display
*/
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
try {
return new BitmapImage(new Uri("../Images/" + (string)(value), UriKind.Relative));
}
catch{
return new BitmapImage();
}
}
/*
Implement this method to modify the target data before sending it back to the source.
*/
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
var img = value as BitmapImage;
return img.UriSource.AbsoluteUri;
}
}
#3
0
You can use Bitmap.FromFile("FileName")
or you can use Image.FromFile("FileName")
.If you want to get an array of Bitmaps you can use a simple linq query
您可以使用Bitmap.FromFile(“FileName”)或者您可以使用Image.FromFile(“FileName”)。如果您想获得一个位图数组,您可以使用简单的linq查询
var fileNames = new string[] { "file1", "file2", "file3" };
var myImages = fileNames.Select(x => Bitmap.FromFile(x)).ToArray();