Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检查图像的宽度和高度_C#_Image_Height_Width - Fatal编程技术网

C# 检查图像的宽度和高度

C# 检查图像的宽度和高度,c#,image,height,width,C#,Image,Height,Width,我可以通过以下代码在图片框中显示图片,而无需检查文件大小: 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;

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

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

将保存图像的高度和宽度

使用
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;
试试看
{
//从系统中获取图像
OpenFileDialog open=新建OpenFileDialog();
打开.Filter=“图像文件(*.jpg;*.jpeg;*.gif;*.bmp)|*.jpg;*.jpeg;*.gif;*.bmp”;
if(open.ShowDialog()==DialogResult.OK)
{
System.IO.FileInfo file=新的System.IO.FileInfo(open.FileName);
位图img=新位图(open.FileName);
如果(img.Width
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;
        }

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

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