gpt4 book ai didi

c# - 限制图像大小

转载 作者:行者123 更新时间:2023-11-30 19:44:23 29 4
gpt4 key购买 nike

我的 WinForm 中有一个 OpenFileDialog,我希望何时选择图像以将图像的大小限制在 100 Ko 和尺寸在 350x350。

我该怎么做??

最佳答案

private bool ValidFile(string filename, long limitInBytes, int limitWidth, int limitHeight)
{
var fileSizeInBytes = new FileInfo(filename).Length;
if(fileSizeInBytes > limitInBytes) return false;

using(var img = new Bitmap(filename))
{
if(img.Width > limitWidth || img.Height > limitHeight) return false;
}

return true;
}

private void selectImgButton_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if(ValidFile(openFileDialog1.FileName, 102400, 350, 350))
{
// Image is valid and U can
// Do something with image
// For example set it to a picture box
pictureBox1.Image = new Bitmap(openFileDialog1.FileName);
}
else
{
MessageBox.Show("Image is invalid");
}
}
}

关于c# - 限制图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12491597/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com