限制图像大小

时间:2012-09-19 09:07:37

标签: c# winforms

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

我该怎么做?

4 个答案:

答案 0 :(得分:4)

这取决于您需要支持的图像类型。对于大多数常见类型(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这样的库

答案 1 :(得分:4)

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 :(得分:0)

将此作为全局变量

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();
}

希望这会有所帮助。

答案 3 :(得分:0)

试试这个:

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);
    }
}