我可以在图片框中显示图片,而无需通过以下代码检查文件大小:
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 。我还想检查图像的宽度和高度。
答案 0 :(得分:35)
Bitmap
将保持图像的高度和宽度。
使用FileInfo
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;
答案 1 :(得分:3)
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");
}
答案 2 :(得分:0)
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;
}
答案 3 :(得分:0)
我遇到了类似的问题,我写了一个方法来检测图片是否为横向。 如果可以帮到你。
struct ContentView: View {
var body: some View {
DrawShapeBorder(points: [
CGPoint(x: 100, y: 150),
CGPoint(x: 300, y: 100),
CGPoint(x: 300, y: 200),
CGPoint(x: 100, y: 200)
])
.stroked()
}
}
struct DrawShapeBorder: Shape {
/// add the double border
func stroked() -> some View {
ZStack {
self.stroke(Color.red, style: StrokeStyle(lineWidth: 20, lineCap: .round, lineJoin: .round))
self.stroke(Color.white, style: StrokeStyle(lineWidth: 10, lineCap: .round, lineJoin: .round))
}
}
var points: [CGPoint]
func path(in rect: CGRect) -> Path {
var path: Path = Path()
guard let startingPoint = points.first else {
return path
}
// go up
let upStartingPoint = CGPoint(x: startingPoint.x, y: startingPoint.y + 5)
path.move(to: upStartingPoint)
for pointLocation in points {
path.addLine(to: pointLocation)
}
return path
}
}