2

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

我怎样才能做到这一点 ??

4

4 回答 4

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 之类的库

于 2012-09-19T09:17:07.623 回答
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");
                }
            }
        }
于 2012-09-19T09:46:48.363 回答
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();
}

希望这会有所帮助。

于 2012-09-19T09:25:07.250 回答
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);
    }
}
于 2012-09-19T09:40:03.500 回答