Check the width and height of an image

后端 未结 3 1763
被撕碎了的回忆
被撕碎了的回忆 2021-01-17 10:10

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, EventAr         


        
相关标签:
3条回答
  • 2021-01-17 10:23

    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;
            }
    
    0 讨论(0)
  • 2021-01-17 10:25
            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");
            }
    
    0 讨论(0)
  • 2021-01-17 10:42

    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;
    
    0 讨论(0)
提交回复
热议问题