void buttonLoad_Click(object sender, EventArgs e) { const string SupportedExtensions = "*.sa4; *.png; *.jpg; *.bmp; *.gif; *.tif"; using (var openDialog = new OpenFileDialog { Filter = "Images (" + SupportedExtensions + ")|" + SupportedExtensions }) { if (openDialog.ShowDialog(this) == DialogResult.OK) { if (Path.GetExtension(openDialog.FileName).Equals(".sa4", StringComparison.OrdinalIgnoreCase)) { using (var stream = new FileStream(openDialog.FileName, FileMode.Open)) { pictureBox1.Image?.Dispose(); pictureBox1.Image = ImageSerializer.LoadFromStream(stream).ToBitmap(); } } else { pictureBox1.Image = new Bitmap(openDialog.FileName); } } } }
void buttonOpen_Click(object sender, EventArgs e) { using (var openFileDialog = new OpenFileDialog()) { openFileDialog.Filter = Resources.FileFilterImages; if (openFileDialog.ShowDialog() == DialogResult.OK) { try { var fileName = openFileDialog.FileName; using (imageBoxSource.Controller?.SuspendUpdateVisualImage()) { if (Path.GetExtension(fileName)?.Equals(".sa4", StringComparison.OrdinalIgnoreCase) ?? false) { CodedImage image; using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { image = ImageSerializer.LoadFromStream(stream); } if (!string.IsNullOrEmpty(image?.SourceImageFileName) && File.Exists(image.SourceImageFileName)) { var choice = MessageBox.Show( string.Format(Resources.WizardLoadSourceImageInsteadOfScheme, fileName, image.SourceImageFileName), Resources.WizardLoadImage, MessageBoxButtons.YesNoCancel); switch (choice) { case DialogResult.Cancel: return; case DialogResult.Yes: fileName = image.SourceImageFileName; RestoreImageSettings(image); break; case DialogResult.No: Wizard.ImageSetter.SetNewImage(image); RestoreImageSettings(image); EnableControls(); return; } } } Wizard.LoadSourceImageFromFile(fileName); } EnableControls(); } catch (Exception ex) { MessageBox.Show(Resources.ErrorCannotOpenFile + Environment.NewLine + ex.Message); } } } }
public void TestSerialize() { var image = new CodedImage { Size = new Size(2, 3), SourceImageFileName = @"c:\Temp\Test Image.jpg" }; image.CompletePalette(); image[0, 0] = new CodedColor(1); image[0, 1] = new CodedColor(100); image[0, 2] = new CodedColor(2); image[1, 0] = new CodedColor(2); image[1, 1] = new CodedColor(100); image[1, 2] = new CodedColor(3); Assert.AreEqual(4, image.Palette.Count, "Precondition"); var tempFileName = Path.GetTempFileName(); try { using (var fs = new FileStream(tempFileName, FileMode.Create)) { image.SaveToStream(fs); } using (var fs = new FileStream(tempFileName, FileMode.Open)) { var reloadedImage = ImageSerializer.LoadFromStream(fs); Assert.AreEqual(new Size(2, 3), reloadedImage.Size); Assert.AreEqual(@"c:\Temp\Test Image.jpg", reloadedImage.SourceImageFileName); Assert.IsNotNull(reloadedImage.Palette); Assert.AreEqual(4, reloadedImage.Palette.Count); Assert.IsTrue(reloadedImage.Palette[1] is CodedColor); Assert.IsTrue(reloadedImage.Palette[2] is CodedColor); Assert.IsTrue(reloadedImage.Palette[3] is CodedColor); Assert.IsTrue(reloadedImage.Palette[100] is CodedColor); Assert.AreEqual(new CodedColor(1), reloadedImage[0, 0]); Assert.AreEqual(new CodedColor(100), reloadedImage[0, 1]); Assert.AreEqual(new CodedColor(2), reloadedImage[0, 2]); Assert.AreEqual(new CodedColor(2), reloadedImage[1, 0]); Assert.AreEqual(new CodedColor(100), reloadedImage[1, 1]); Assert.AreEqual(new CodedColor(3), reloadedImage[1, 2]); } } finally { if (File.Exists(tempFileName)) { File.Delete(tempFileName); } } }
public static IndexedImage GetImageResource(string resourceName) { using (var stream = GetRawResourceStream(resourceName)) { if (stream != null) { return(ImageSerializer.LoadFromStream(stream)); } } return(null); }
void homeUserControl_OpenButtonClicked(object sender, HomeUserControl.OpenFileEventArgs e) { try { CodedImage image; using (var stream = new FileStream(e.FileName, FileMode.Open, FileAccess.Read)) { image = ImageSerializer.LoadFromStream(stream); image.FileName = e.FileName; Settings.Default.AddLastOpenFile(e.FileName); } ShowSchemeUserControl(image); homeUserControl?.Dispose(); } catch (Exception ex) { MessageBox.Show(Resources.ErrorCannotOpenFile + Environment.NewLine + ex.Message); } }
void InitializeRecentFilesButtons(ToolTip toolTip) { if (string.IsNullOrEmpty(Settings.Default.LastOpenFiles)) { var startupPath = Path.Combine(AppInfo.StartupPath, "Samples"); if (!string.IsNullOrEmpty(startupPath) && Directory.Exists(startupPath)) { var sampleFileNames = Directory.GetFiles(startupPath, "*.sa4").Take(10); Settings.Default.LastOpenFiles = string.Join(Settings.FilesSeparator.ToString(), sampleFileNames); Settings.Default.Save(); } } foreach (var fileName in Settings.Default.LastOpenFiles.Split(Settings.FilesSeparator)) { if (File.Exists(fileName)) { IndexedImage image; try { using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { image = ImageSerializer.LoadFromStream(stream); } } catch (IOException) { continue; } if (image != null && image.Size.Width > 0 && image.Size.Height > 0) { Bitmap bitmap; if (image.Size.Width > 200 || image.Size.Height > 200) { var maxLength = Math.Max(image.Size.Width, image.Size.Height); var newSize = new Size(image.Size.Width * 200 / maxLength, image.Size.Height * 200 / maxLength); bitmap = new ImageResampler().Resample(image, newSize, ImageResampler.FilterType.Box).ToBitmap(); } else { bitmap = image.ToBitmap(); } var imageButton = new FlatButton(); imageButton.Size = new System.Drawing.Size(250, 250); imageButton.Image = bitmap; imageButton.Text = Environment.NewLine + Path.GetFileNameWithoutExtension(fileName); imageButton.Tag = fileName; imageButton.TextAlign = ContentAlignment.BottomCenter; imageButton.ImageAlign = ContentAlignment.MiddleCenter; imageButton.FlatAppearance.BorderSize = 0; imageButton.Click += ImageButton_Click; var tooltip = fileName + Environment.NewLine + string.Format(Resources.ImageInfoTooltip, image.Size.Width, image.Size.Height, image.Palette.Count); toolTip.SetToolTip(imageButton, tooltip); panelLastOpenFiles.Controls.Add(imageButton); } } } }