Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取图像属性?_C#_Winforms - Fatal编程技术网

C# 如何获取图像属性?

C# 如何获取图像属性?,c#,winforms,C#,Winforms,最重要的属性是图像的高度和宽度,尽管还需要其他属性 我尝试了以下代码: private void getImageProperties() { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.ShowDialog(); Dictionary<int, KeyValuePair<string, string>> fileProps = GetFileProps(o

最重要的属性是图像的高度和宽度,尽管还需要其他属性

我尝试了以下代码:

private void getImageProperties()
{
  OpenFileDialog openFileDialog = new OpenFileDialog();
  openFileDialog.ShowDialog();
  Dictionary<int, KeyValuePair<string, string>> fileProps = 
    GetFileProps(openFileDialog.FileName);

  foreach (KeyValuePair<int, KeyValuePair<string, string>> kv in fileProps)
    Console.WriteLine(kv.ToString());
}
但是什么是GetFileProps?它不存在。

修改以提供更完整的示例:

代码中缺少一个方法。你可以自己重新制作

使用System.Drawing命名空间:

Dictionary<int, KeyValuePair<string, string>> GetFileProps(string path)
{
    System.Drawing.Image image = System.Drawing.Image.FromFile(path);

    var dictionary = new Dictionary<int, KeyValuePair<string, string>>();
    dictionary.Add(1, new KeyValuePair<string, string>("Width", image.Width.ToString()));
    dictionary.Add(2, new KeyValuePair<string, string>("Height", image.Height.ToString()));

    //Implement the rest of the properties you deem important here.

    return dictionary;
}
修改以提供更完整的示例:

代码中缺少一个方法。你可以自己重新制作

使用System.Drawing命名空间:

Dictionary<int, KeyValuePair<string, string>> GetFileProps(string path)
{
    System.Drawing.Image image = System.Drawing.Image.FromFile(path);

    var dictionary = new Dictionary<int, KeyValuePair<string, string>>();
    dictionary.Add(1, new KeyValuePair<string, string>("Width", image.Width.ToString()));
    dictionary.Add(2, new KeyValuePair<string, string>("Height", image.Height.ToString()));

    //Implement the rest of the properties you deem important here.

    return dictionary;
}
下面是GetFileProps:

但这需要添加一些参考资料。有关更多信息,请检查我复制方法的位置。请开始使用谷歌

下面是GetFileProps:

但这需要添加一些参考资料。有关更多信息,请检查我复制方法的位置。请开始使用谷歌

你可以试试这个

                string path = "Path of image";
                Bitmap bmp = new Bitmap(path);
                StringBuilder sb = new StringBuilder();
                FileInfo fi = new FileInfo(path);
                sb.AppendLine("Name : " + fi.Name);
                sb.AppendLine("Width : " + bmp.Width);
                sb.AppendLine("Height : " + bmp.Height);
                sb.AppendLine("Horizontal Resolution : " + bmp.HorizontalResolution);
                sb.AppendLine("Vertical Resolution : " + bmp.VerticalResolution);
                string type = "";
                if (fi.Extension == ".bmp")
                {
                    type = "Bitmap Image";
                }
                else if (fi.Extension == ".jpg" || fi.Extension == ".jpeg")
                {
                    type = "Joint Photographic Experts Group Image File";
                }
                else if (fi.Extension == ".png")
                {
                    type = "Portable Network Graphic Image";
                }
                sb.AppendLine("Type : " + type);
                bmp.Dispose();
                MessageBox.Show(sb.ToString(), path, MessageBoxButtons.OK, MessageBoxIcon.Information);
这适用于任何.bmp、.png、.jpg、.jpeg文件,但您可以添加更多文件,如果需要,这是一个示例项目

希望有帮助。

你可以试试这个

                string path = "Path of image";
                Bitmap bmp = new Bitmap(path);
                StringBuilder sb = new StringBuilder();
                FileInfo fi = new FileInfo(path);
                sb.AppendLine("Name : " + fi.Name);
                sb.AppendLine("Width : " + bmp.Width);
                sb.AppendLine("Height : " + bmp.Height);
                sb.AppendLine("Horizontal Resolution : " + bmp.HorizontalResolution);
                sb.AppendLine("Vertical Resolution : " + bmp.VerticalResolution);
                string type = "";
                if (fi.Extension == ".bmp")
                {
                    type = "Bitmap Image";
                }
                else if (fi.Extension == ".jpg" || fi.Extension == ".jpeg")
                {
                    type = "Joint Photographic Experts Group Image File";
                }
                else if (fi.Extension == ".png")
                {
                    type = "Portable Network Graphic Image";
                }
                sb.AppendLine("Type : " + type);
                bmp.Dispose();
                MessageBox.Show(sb.ToString(), path, MessageBoxButtons.OK, MessageBoxIcon.Information);
这适用于任何.bmp、.png、.jpg、.jpeg文件,但您可以添加更多文件,如果需要,这是一个示例项目


希望有帮助。

图像也可以包含EXIF数据。你在找吗?GetFileProps是一种你错过了代码的方法,试着问问编写此代码的人吧?图像也可以有EXIF数据。你在找吗?GetFileProps是一种方法,你错过了代码,试着问一下编写这段代码的人吧?我想我被否决了。我编辑了我的答案,使之更完整。我想我被否决了。我编辑了我的答案,使之更加完整。