C# 文件上载时调整图像大小
我在我的wpf mvvm应用程序中尝试将图像上载到数据库。代码工作正常,图像作为图像保存到db。我需要在上载时实现图像大小调整。我的意思是,单击“上载”时,图像将动态调整大小并保存到db 这是我的密码C# 文件上载时调整图像大小,c#,wpf,image,mvvm,C#,Wpf,Image,Mvvm,我在我的wpf mvvm应用程序中尝试将图像上载到数据库。代码工作正常,图像作为图像保存到db。我需要在上载时实现图像大小调整。我的意思是,单击“上载”时,图像将动态调整大小并保存到db 这是我的密码 public void Upload(object obj) { try { Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); dlg.Default
public void Upload(object obj)
{
try
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.DefaultExt = ".png";
dlg.Filter = "Image files (*.png;*.jpg)|*.png;*.jpg";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
string filename = dlg.FileName;
UploadText = filename;
FileStream FS = new FileStream(filename, FileMode.Open, FileAccess.Read);
byte[] img = new byte[FS.Length];
FS.Read(img, 0, Convert.ToInt32(FS.Length));
UploadLogo = img;
Stream reader = File.OpenRead(filename);
System.Drawing.Image photo = System.Drawing.Image.FromStream((Stream)reader);
MemoryStream finalStream = new MemoryStream();
photo.Save(finalStream, ImageFormat.Png);
// translate to image source
PngBitmapDecoder decoder = new PngBitmapDecoder(finalStream, BitmapCreateOptions.PreservePixelFormat,
BitmapCacheOption.Default);
ClientLogo = decoder.Frames[0]; ;
}
}
catch (Exception ex)
{
throw ex;
}
}
请帮忙
提前感谢我不知道您正在为哪个平台编程,但我知道有一种方法需要添加一个复选框来测量UIImage的宽度和高度。如果大于某个像素,可以借助UIGraphics调整其大小。它应该是这样的:
if (image.Size.Width >= 1000 || image.Size.Height >= 1000)
{
var width = images [i].Size.Width;
var height = images [i].Size.Height;
var newWidth = 900;
var newHeigth = height * newWidth / width;
UIGraphics.BeginImageContext (new SizeF (newWidth, newHeigth));
image.Draw (new RectangleF (0, 0, newWidth, newHeigth));
image = UIGraphics.GetImageFromCurrentImageContext ();
UIGraphics.EndImageContext ();
}
同样,我不知道这种方法是否适用于您正在编程的平台,但您可以尝试一下
希望这有帮助。祝你好运