【问题标题】:Check the width and height of an image检查图像的宽度和高度
【发布时间】:2011-08-28 00:27:21
【问题描述】:

我可以通过以下代码在图片框中显示图片而无需检查文件大小:

private void button3_Click_1(object sender, EventArgs e)
{
    try
    {
        //Getting The Image From The System
        OpenFileDialog open = new OpenFileDialog();
        open.Filter =
          "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";

        if (open.ShowDialog() == DialogResult.OK)
        {
            Bitmap img = new Bitmap(open.FileName);

            pictureBox2.Image = img;
        }
    }
    catch (Exception)
    {
        throw new ApplicationException("Failed loading image");
    }
}

我想在图片框中显示之前检查图像大小,例如它是 2MB 还是 4MB。我还想检查图像的 widthheight

【问题讨论】:

    标签: c# image height width


    【解决方案1】:

    Bitmap 将保存图像的高度和宽度。

    使用FileInfo Length 属性获取文件大小。

    FileInfo file = new FileInfo(open.FileName);
    var sizeInBytes = file.Length;
    
    Bitmap img = new Bitmap(open.FileName);
    
    var imageHeight = img.Height;
    var imageWidth = img.Width;
    
    pictureBox2.Image = img;
    

    【讨论】:

      【解决方案2】:
              try
              {
                  //Getting The Image From The System
                  OpenFileDialog open = new OpenFileDialog();
                  open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
                  if (open.ShowDialog() == DialogResult.OK)
                  {
                      System.IO.FileInfo file = new System.IO.FileInfo(open.FileName);
                      Bitmap img = new Bitmap(open.FileName);
      
      
                      if (img.Width < MAX_WIDTH &&
                          img.Height < MAX_HEIGHT &&
                          file.Length < MAX_SIZE)
                          pictureBox2.Image = img;
      
                  }
              }
              catch (Exception)
              {
                  throw new ApplicationException("Failed loading image");
              }
      

      【讨论】:

        【解决方案3】:

        UWP 目前有一个很好的接口来获取图像属性。

                FileOpenPicker openPicker = new FileOpenPicker();
                openPicker.ViewMode = PickerViewMode.Thumbnail;
                openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
                openPicker.FileTypeFilter.Add(".jpg");
                openPicker.FileTypeFilter.Add(".jpeg");
                openPicker.FileTypeFilter.Add(".png");
        
                StorageFile file = await openPicker.PickSingleFileAsync();
                if (file != null)
                {
                    // Application now has read/write access to the picked file
                    ImageProperties IP = await file.Properties.GetImagePropertiesAsync();
        
                    double Width = IP.Width;
                    double Height = IP.Height;
                }
        

        【讨论】:

          【解决方案4】:

          我有一个类似的问题,我写了一个方法来检测图片是否是风景。 如果它可以帮助你。

          public static bool IsPictureLandscape(string fileName)
          {
            try
            {
              Bitmap image = new Bitmap(fileName);
              return image.Width > image.Height;
            }
            catch (Exception)
            {
              return false;
            }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2014-12-13
            • 1970-01-01
            • 1970-01-01
            • 2016-08-09
            • 1970-01-01
            • 1970-01-01
            • 2010-11-18
            相关资源
            最近更新 更多