private void Button_LoadImage(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog openFileDialog1 = new Microsoft.Win32.OpenFileDialog();
// openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
openFileDialog1.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp; *.png)|*.jpg; *.jpeg; *.gif; *.bmp; *.png";
openFileDialog1.RestoreDirectory = true;
Nullable<bool> result = openFileDialog1.ShowDialog(); // Show the dialog.
if (result == true) // Test result.
{
try
{
BitmapImage image = new BitmapImage();
image.BeginInit();
image.UriSource = new Uri(openFileDialog1.FileName);
image.CacheOption = BitmapCacheOption.OnLoad;
image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
image.EndInit();
BitmapSource a = ConvertBitmapTo96DPI(image);
int width = (int)a.Width;
int height = (int)a.Height;
if (width > 1500 || image.Height > 1500)
{
string size = width.ToString() + "x" + height.ToString();
int newWidth = 1500;
float scale = (float)newWidth / ((float)width / (float)height);
int newHeight = Convert.ToInt32(scale);
MessageBoxResult answer = MessageBox.Show("Image size is " + size + ", "
+ "such a big image could increase A LOT the BCF file size. "
+ "Do you want me to resize it to " + newWidth.ToString() + "x" + newHeight.ToString() + " for you?", "Attention!",
MessageBoxButton.YesNo, MessageBoxImage.Question);
if (answer == MessageBoxResult.Yes)
{
width = newWidth;
}
/*
TaskDialog mainDialog = new TaskDialog("Attention!");
mainDialog.MainInstruction = "Attention!";
mainDialog.MainContent =
"Image size is " + size + ", "
+ "such a big image could increase A LOT the BCF file size. "
+ "Do you want me to resize it to " + newWidth.ToString() + "x" + newHeight.ToString() + " for you?";
mainDialog.CommonButtons = TaskDialogCommonButtons.No ^ TaskDialogCommonButtons.Yes;
mainDialog.DefaultButton = TaskDialogResult.Yes;
TaskDialogResult tResult = mainDialog.Show();
//ONLY IF NECESSARY I RESIZE
if (TaskDialogResult.Yes == tResult)
{
width = newHeight;
}*/
}
byte[] imageBytes = LoadImageData(openFileDialog1.FileName);
ImageSource imageSource = CreateImage(imageBytes, width, 0);
imageBytes = GetEncodedImageData(imageSource, ".jpg");
SaveImageData(imageBytes, snapshot);
//VISUALIZE IMAGE
BitmapImage source = new BitmapImage();
source.BeginInit();
source.UriSource = new Uri(snapshot);
source.CacheOption = BitmapCacheOption.OnLoad;
source.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
source.EndInit();
SnapshotImg.Source = source;
PathLabel.Content = openFileDialog1.FileName;
}
catch (System.Exception ex1)
{
TaskDialog.Show("Error!", "exception: " + ex1);
}
}
}