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