繁体   English   中英

是否存在从字符串文本(参数即文件名和路径)和fileinfo提供给Image类型的C#.NET内置转换?

[英]Is there a C# .NET built-in conversion from a text of string(parameter i.e. filename and path) and fileinfo provided to an Image type?

问题:

您知道.NET中的哪些内容可以转换我的文本(字符串)并返回图像类型吗? 像.Net的“转换”类一样,但它不支持图像转换。 我的意思是..就像传递文件信息(文件名和路径)作为参数并返回要显示的图像(位图)一样。 我真的必须手动编码吗?

场景:

我已成功从目录(从闪存驱动器或本地驱动器)中收集了一些图像文件列表,并希望将其显示为实际图像。

希望我的问题很清楚。

实现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;
        }

    }

根据您使用的是WinForms还是WPF,可以使用System.Drawing.ImageSystem.Windows.Media.ImageSource

您不能将字符串转换为屏幕上的图像。 您必须将其加载到另一个组件中。 例如, Image具有静态的FromFile(string filepath)方法,该方法可以加载图像并使之可供显示。

您可以使用Bitmap.FromFile("FileName")Image.FromFile("FileName") 。如果要获取Bitmap的数组,可以使用简单的linq查询

 var fileNames = new string[] { "file1", "file2", "file3" };
 var myImages = fileNames.Select(x => Bitmap.FromFile(x)).ToArray();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM