【问题标题】:Limit image size限制图像大小
【发布时间】:2012-09-19 09:07:37
【问题描述】:

我的 WinForm 中有一个 OpenFileDialog,我想何时选择图像以将图像的大小限制为 100 Ko,将尺寸限制为 350x350。

我该怎么做??

【问题讨论】:

  • 你试过什么?您遇到了哪些具体的代码问题?
  • 我对代码没有任何问题,我只是不知道如何添加允许我限制图像大小和尺寸的功能。

标签: c# winforms


【解决方案1】:
private bool ValidFile(string filename, long limitInBytes, int limitWidth, int limitHeight)
        {
            var fileSizeInBytes = new FileInfo(filename).Length;
            if(fileSizeInBytes > limitInBytes) return false;

            using(var img = new Bitmap(filename))
            {
                if(img.Width > limitWidth || img.Height > limitHeight) return false;
            }

            return true;
        }

        private void selectImgButton_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if(ValidFile(openFileDialog1.FileName, 102400, 350, 350))
                {
                    // Image is valid and U can
                    // Do something with image
                    // For example set it to a picture box
                    pictureBox1.Image = new Bitmap(openFileDialog1.FileName);
                }
                else
                {
                    MessageBox.Show("Image is invalid");
                }
            }
        }

【讨论】:

    【解决方案2】:

    这取决于您需要支持的图像类型。对于最常见的类型(bmp、jpg、png),您可以轻松检索图像信息:

    string filename = // get it from OpenFileDialog
    
    if (new FileInfo(filename).Length > SOME_LIMIT)
    {
      MessageBox.Show("!!!");
    }
    else
    {
      Image img = Image.FromFile(filename);
      MessageBox.Show(string.Format("{0} x {1}", img.Width, img.Height));
    }
    

    如果您需要对许多图像格式提供更广泛的支持,那么我建议您使用像 ImageMagick.NET 这样的库

    【讨论】:

      【解决方案3】:

      把它作为全局变量

      int imgSize = 0


      private void button1_Click(object sender, EventArgs e)
      {
        Image imageFile;
        OpenFileDialog dlg = new OpenFileDialog();
      
        dlg.Title = "Open Image";
        dlg.Filter = "jpg files (*.jpg)|*.jpg|All files (*.*)|*.*";
      
        if (dlg.ShowDialog() == DialogResult.OK)
           {
            imageFile = Image.FromFile(dlg.FileName);
            imgHeight = imageFile.Height;
            if (imgHeight > 350)
               {
                    MessageBox.Show("Not 350x350 Image", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                      imgPhoto.Image = null;
               }
               else
               {
                 PictureBox1.Image = new Bitmap(dlg.OpenFile());
               }
            }
        dlg.Dispose();
      }
      

      希望这会有所帮助。

      【讨论】:

      • @Spoon Yukina,如果我理解你的问题,你想将图像从原始尺寸修改为 350x350 吗?
      【解决方案4】:

      试试这个:

      OpenFileDialog fileDialog = new OpenFileDialog
      {
          // managed GDI+ supports bmp, jpeg, gif, png and tiff.
          Filter =
              "Image files (*.bmp;*.jpg;*.gif;*.png;*.tiff)|*.bmp;*.jpg;*.gif;*.png;*.tiff|All files (*.*)|*.*",
      };
      if (fileDialog.ShowDialog() == DialogResult.OK)
      {
          // Many exceptions could be raised in the following code
          try
          {
              var fileSize = new FileInfo(fileDialog.FileName);
              var validFilesize = fileSize.Length <= 1024 * 100; // 100 kilo bytes
              var validDimensions = false;
      
              // free the file once we get the dimensions
              using (Image image = Image.FromFile(fileDialog.FileName))
              {
                  validDimensions = (image.Width <= 350) && (image.Height <= 350);
              }
      
              if (!validDimensions || !validFilesize)
              {
                  MessageBox.Show("Error ! Choose another image");
              } 
              else
              {
                  // do something with the file here
              }
          }
          catch (Exception exception)
          {
              MessageBox.Show(exception.Message);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2018-02-07
        • 2013-04-05
        • 2014-06-17
        • 2012-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-25
        • 1970-01-01
        相关资源
        最近更新 更多