I my wpf mvvm application I am trying to upload an image to the database.The code is working fine and the image save to the db as image.I need to implement resize the image while upload.I mean When click upload the image will resize dynamically and save to db Here is my code
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;
}
}
Please help
Thanks in advance
I don't know exactly for which platform you're programming, but I know of a method that involves adding a check, to measure the width and height of an UIImage. If it's greater than a certain pixels, you can resize it with help of the UIGraphics. It should look something like this:
Again, I don't know if this method works for the platform you're programming, but you could give it a try.
Hope this helps. Good luck!