/// <summary> /// Creates a new Image. /// </summary> public void New() { // Creating new Image with the specified Size: SizeSettings sizeSettings = new SizeSettings(); sizeSettings.DataContext = new Point(200, 200); if (sizeSettings.ShowDialog() == true) { int width = (int)((Point)sizeSettings.DataContext).X; int height = (int)((Point)sizeSettings.DataContext).Y; if (width <= 0) { width = 1; } if (height <= 0) { height = 1; } EditedImage image = ImageHelper.New(width, height); // Creating Canvas for the Image: CanvasViewModel canvas = new CanvasViewModel(image) { Name = string.Format(StringResources.NewImageNameFormat, ++this.canvasIndex), Toolbox = this.toolbox, IsChanged = false }; // Adding Canvas into the Editor. this.Canvases.Add(canvas); this.CurrentCanvas = canvas; } }
/// <summary> /// Saves Application's internal Image Format. /// </summary> /// <param name="file">File to save into.</param> /// <param name="image">Image to save.</param> public static void SaveMyImage(FileInfo file, EditedImage image) { XmlDocument document = new XmlDocument(); XmlElement root = document.CreateElement("MyImage"); document.AppendChild(root); // Saving Image Width and Height into Root Node: root.SetAttribute("Width", image.Width.ToString()); root.SetAttribute("Height", image.Height.ToString()); // Adding Layers into the Document: for (int i = 0; i < image.Layers.Count; i++) { XmlElement layer = document.CreateElement("Layer"); layer.SetAttribute("Name", image.Layers[i].Name); layer.SetAttribute("Visible", image.Layers[i].Visible.ToString()); // Converting Image into a String: using (MemoryStream output = new MemoryStream()) { PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(image.Layers[i].Image)); encoder.Save(output); byte[] bytes = output.ToArray(); layer.InnerText = Convert.ToBase64String(bytes); } root.AppendChild(layer); } // Saving the whole Document: document.Save(file.FullName); }
/// <summary> /// Opens an existing Image. /// </summary> public void Open() { FileInfo file = null; // Selecting a File to open: if (DialogHelper.ShowOpenFileDialog(out file)) { // Opening specified Image: EditedImage image = ImageHelper.Open(file); if (image == null) { return; } // Creating Canvas for the Image: CanvasViewModel canvas = new CanvasViewModel(image) { File = file, Toolbox = this.toolbox, IsChanged = false }; // Adding Canvas into the Editor: this.Canvases.Add(canvas); this.CurrentCanvas = canvas; } }
/// <summary> /// Saves the specified Image File. /// </summary> /// <param name="file">File to save into.</param> /// <param name="image">Image to save.</param> public static void Save(FileInfo file, EditedImage image) { try { // Selecting a Way to save the Image depending on it's Format: switch (file.Extension.ToLower()) { case ".mim": SaveMyImage(file, image); break; case ".bmp": SaveBitmap(file, image, new BmpBitmapEncoder()); break; case ".png": SaveBitmap(file, image, new PngBitmapEncoder()); break; case ".jpg": case ".jpeg": SaveBitmap(file, image, new JpegBitmapEncoder()); break; case ".gif": SaveBitmap(file, image, new GifBitmapEncoder()); break; case ".tif": SaveBitmap(file, image, new TiffBitmapEncoder()); break; default: throw new ArgumentException(StringResources.UnsupportedFileTypeMessage); } } catch (Exception ex) { DialogHelper.ShowCriticalError(ex.Message); } }
/// <summary> /// Saves Image to the specified Bitmap File. /// </summary> /// <param name="file">File to save into.</param> /// <param name="image">Image to save.</param> /// <param name="encoder">Encoder used to save the File.</param> private static void SaveBitmap(FileInfo file, EditedImage image, BitmapEncoder encoder) { encoder.Frames.Add(BitmapFrame.Create(image.Image)); FileStream output = File.Open(file.FullName, FileMode.OpenOrCreate, FileAccess.Write); encoder.Save(output); output.Close(); }
public override void Process(EditedImage image) { image.CurrentLayer.Image = ImageHelper.CreateRenderTarget( (int)image.CurrentLayer.Image.Width, (int)image.CurrentLayer.Image.Height, (visual, context) => { visual.Effect = this.effect.Value; context.DrawImage(image.CurrentLayer.Image, new Rect(0.0, 0.0, image.CurrentLayer.Image.Width, image.CurrentLayer.Image.Height)); }); }
public override void Process(EditedImage image) { SizeSettings settings = new SizeSettings(); settings.DataContext = new Point(image.Width, image.Height); if (settings.ShowDialog() == true) { image.Resize((Point)settings.DataContext); } }
private async void buttonShare_Click(object sender, RoutedEventArgs e) { var popup = new ToolTip(); popup.BorderBrush = new SolidColorBrush(Colors.LightGray); popup.BorderThickness = new Thickness(2); popup.Background = new SolidColorBrush(Colors.Black); var stack = new StackPanel(); stack.Margin = new Thickness(5); var text = new TextBlock() { Text = "Uploading image...", Foreground = new SolidColorBrush(Colors.White), FontSize = 20, Margin = new Thickness(0, 5, 0, 5) }; var progress = new ProgressBar() { Foreground = Foreground = new SolidColorBrush(Colors.SteelBlue), Margin = new Thickness(0, 5, 0, 5), Height = 15 }; stack.Children.Add(text); stack.Children.Add(progress); popup.Content = stack; popup.PlacementTarget = this; popup.Placement = PlacementMode.Center; popup.IsOpen = true; var savePath = Path.Combine(Path.GetTempPath(), "CleanShot_Image.png"); EditedImage.Save(savePath, ImageFormat.Png); var client = new System.Net.WebClient(); client.UploadProgressChanged += (send, arg) => { progress.Value = arg.ProgressPercentage; }; #if DEBUG var url = "https://localhost:44355/ImageShare"; #else var url = "https://lucency.co/ImageShare"; #endif byte[] response = new byte[0]; try { response = await client.UploadFileTaskAsync(new Uri(url), savePath); } catch (System.Net.WebException) { popup.IsOpen = false; MessageBox.Show("There was a problem uploading the image. Your internet connection may not be working, or the web service may be temporarily unavailable.", "Upload Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } var strResponse = Encoding.UTF8.GetString(response); popup.IsOpen = false; Process.Start(strResponse); }
/// <summary> /// Creates a new Image with the specified Size. /// </summary> /// <param name="width">Width of the new Image.</param> /// <param name="height">Height of the new Image.</param> /// <returns>New Image Instance.</returns> public static EditedImage New(int width, int height) { // Creating an Image: EditedImage image = new EditedImage() { Width = width, Height = height }; // Adding default Layer: image.AddLayer(); return(image); }
/// <summary> /// Loads Image from the specified Bitmap File. /// </summary> /// <param name="file">File to load From.</param> /// <returns>Loaded Image Instance.</returns> private static EditedImage LoadBitmap(FileInfo file) { BitmapImage bitmap = new BitmapImage(new Uri(file.FullName)); EditedImage image = new EditedImage(); image.Width = (int)bitmap.Width; image.Height = (int)bitmap.Height; image.AddLayer(); image.CurrentLayer.Image = bitmap; return(image); }
private void buttonSave_Click(object sender, RoutedEventArgs e) { var dialog = new Microsoft.Win32.SaveFileDialog(); dialog.InitialDirectory = Settings.Current.SaveFolder; dialog.AddExtension = true; dialog.Filter = "Image Files (*.png)|*.png"; dialog.DefaultExt = ".png"; dialog.ShowDialog(); if (!String.IsNullOrWhiteSpace(dialog.FileName)) { EditedImage.Save(dialog.FileName); } }
public override void Process(EditedImage image) { this.editedImage = image; this.original = this.editedImage.CurrentLayer.Image; this.Brightness = 0.0f; this.Contrast = 1.0f; // Showing Brightness/Contrast Settings: BrightnessContrastSettings settings = new BrightnessContrastSettings(); settings.DataContext = this; if (settings.ShowDialog() == false) { // Cancelling all the made Changes: this.editedImage.CurrentLayer.Image = this.original; } }
private async Task <EditedImage> AttachEditedImage(MemoryStream stream, Domain.Image image, ImageOperation operation, int width, int height) { var physicalFile = await new StoreFileDto { File = stream, Filename = image.Filename }.CreatePhysicalFile(); var editedImage = new EditedImage { Width = width, Height = height, ImageId = image.Id, Operation = operation, Path = physicalFile.Path, }; context.Add(editedImage); await context.SaveChangesAsync(); return(editedImage); }
/// <summary> /// Loads Application's internal Image Format. /// </summary> /// <param name="file">File to open.</param> /// <returns>Loaded Image Instance.</returns> public static EditedImage LoadMyImage(FileInfo file) { EditedImage image = new EditedImage(); // Loading Document: XmlDocument document = new XmlDocument(); document.Load(file.FullName); // Getting Image Width and Height from Root Node: XmlNode root = document.GetElementsByTagName("MyImage")[0]; image.Width = int.Parse(root.Attributes["Width"].Value); image.Height = int.Parse(root.Attributes["Height"].Value); // Getting Layers: XmlNodeList layerNodes = document.GetElementsByTagName("Layer"); // Adding Layers in back Direction: for (int i = layerNodes.Count - 1; i >= 0; i--) { image.AddLayer(); // Loading Layer Image: byte[] imageBytes = Convert.FromBase64String(layerNodes[i].InnerText); using (MemoryStream input = new MemoryStream(imageBytes)) { BitmapImage bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.StreamSource = input; bitmap.EndInit(); image.CurrentLayer.Image = bitmap; } image.CurrentLayer.Name = layerNodes[i].Attributes["Name"].Value; image.CurrentLayer.Visible = bool.Parse(layerNodes[i].Attributes["Visible"].Value); } return(image); }
public override void Process(EditedImage image) { RotationSettings settings = new RotationSettings(); if (settings.ShowDialog() == true) { // Selecting rotation Angle: RotateTransform rotation = new RotateTransform(); if (settings.rotate90Right.IsChecked == true) { rotation.Angle = 90; } else if (settings.rotate180.IsChecked == true) { rotation.Angle = 180; } else if (settings.rotate90Left.IsChecked == true) { rotation.Angle = 270; } else { return; } // Computing Rotation Center: rotation.CenterX = image.Width / 2; rotation.CenterY = image.Height / 2; // Rotating current Layer's Image: image.CurrentLayer.Image = ImageHelper.CreateRenderTarget((int)image.CurrentLayer.Image.Width, (int)image.CurrentLayer.Image.Height, (visual, context) => { context.DrawImage(image.CurrentLayer.Image, new Rect(0.0, 0.0, image.CurrentLayer.Image.Width, image.CurrentLayer.Image.Height)); visual.Transform = rotation; }); } }
/// <summary> /// Processes the Image in custom Way. /// </summary> /// <param name="image">Image to process.</param> public abstract void Process(EditedImage image);
/// <summary> /// Initializes a new Instance of current Class. /// </summary> /// <param name="editedImage">Image the Canvas is created for.</param> public CanvasViewModel(EditedImage editedImage) { this.EditedImage = editedImage; // Subscribing for Image Events: editedImage.PropertyChanged += this.editedImage_PropertyChanged; }
private void buttonCreateMeme_Click(object sender, RoutedEventArgs e) { var meme = new Meme(); meme.textTop.Text = TopText; meme.textBottom.Text = BottomText; meme.Owner = this; meme.ShowDialog(); if (String.IsNullOrWhiteSpace(TopText) && String.IsNullOrWhiteSpace(BottomText)) { return; } var fontFamily = System.Drawing.FontFamily.Families.FirstOrDefault(ff => ff.Name == FontName); if (fontFamily == null) { fontFamily = System.Drawing.FontFamily.GenericSansSerif; } EditedImage = (Bitmap)OriginalImage.Clone(); Graphic = Graphics.FromImage(EditedImage); Graphic.SmoothingMode = SmoothingMode.AntiAlias; int pointSize = 1; Font font = new Font(fontFamily, pointSize, System.Drawing.FontStyle.Bold); SizeF lastMeasurement = SizeF.Empty; System.Drawing.Point drawPoint = System.Drawing.Point.Empty; GraphicsPath path = new GraphicsPath(); if (!String.IsNullOrWhiteSpace(TopText)) { while (lastMeasurement.Width < SourceSize.Width - 25 && pointSize < Settings.Current.MemeMaxFontSize) { pointSize++; font = new Font(fontFamily, pointSize, System.Drawing.FontStyle.Bold); lastMeasurement = Graphic.MeasureString(TopText, font); } pointSize--; font = new Font(fontFamily, pointSize, System.Drawing.FontStyle.Bold); lastMeasurement = Graphic.MeasureString(TopText, font); drawPoint = new System.Drawing.Point((int)(SourceSize.Width / 2), 0); path = new GraphicsPath(); path.AddString(TopText, fontFamily, (int)System.Drawing.FontStyle.Bold, Graphic.DpiY * pointSize / 72, drawPoint, new StringFormat() { Alignment = StringAlignment.Center }); Graphic.DrawPath(new System.Drawing.Pen(System.Drawing.Brushes.Black, pointSize / 4), path); Graphic.FillPath(System.Drawing.Brushes.White, path); pointSize = 1; font = new Font(fontFamily, pointSize, System.Drawing.FontStyle.Bold); lastMeasurement = SizeF.Empty; } if (!String.IsNullOrWhiteSpace(BottomText)) { while (lastMeasurement.Width < SourceSize.Width - 25 && pointSize < Settings.Current.MemeMaxFontSize) { pointSize++; font = new Font(fontFamily, pointSize, System.Drawing.FontStyle.Bold); lastMeasurement = Graphic.MeasureString(BottomText, font); } pointSize--; font = new Font(fontFamily, pointSize, System.Drawing.FontStyle.Bold); lastMeasurement = Graphic.MeasureString(BottomText, font); drawPoint = new System.Drawing.Point((int)(SourceSize.Width / 2), (int)SourceSize.Height - (int)lastMeasurement.Height); path = new GraphicsPath(); path.AddString(BottomText, fontFamily, (int)System.Drawing.FontStyle.Bold, Graphic.DpiY * pointSize / 72, drawPoint, new StringFormat() { Alignment = StringAlignment.Center }); Graphic.DrawPath(new System.Drawing.Pen(System.Drawing.Brushes.Black, pointSize / 4), path); Graphic.FillPath(System.Drawing.Brushes.White, path); } Graphic.Save(); using (var ms = new MemoryStream()) { EditedImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png); ImageSourceFrame = BitmapFrame.Create(ms, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); imageMain.Source = ImageSourceFrame; } }