检查图像的宽度和高度

发布于 2024-11-08 04:53:41 字数 706 浏览 0 评论 0原文

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

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。我还想检查图像的宽度高度

I am able to display the picture in the picture box without checking the file size by the following code:

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

I want to check the image size for example whether it is 2MB or 4MB before displaying in the picture box. i also want to check the width and height of the image.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

再见回来 2024-11-15 04:53:41

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

使用 FileInfo < code>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;

The Bitmap will hold the height and width of the image.

Use the FileInfo Length property to get the file size.

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;
漫漫岁月 2024-11-15 04:53:41
        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");
        }
        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");
        }
毁梦 2024-11-15 04:53:41

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

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

I had a similar issue and I wrote a method to detect if the picture is landscape or not.
If it can help you.

public static bool IsPictureLandscape(string fileName)
{
  try
  {
    Bitmap image = new Bitmap(fileName);
    return image.Width > image.Height;
  }
  catch (Exception)
  {
    return false;
  }
}
深空失忆 2024-11-15 04:53:41

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

UWP has currently a nice interface to obtain image properties.

        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;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文