Exemple #1
0
        private static RenderTargetBitmap CreateWatermarkedBitmapEncoder(BitmapImage SourceImage, TextWatermarkListWithSerchByUiLabel Watermarks, int newPixelWidth, int newPixelHeight)
        {
            DrawingVisual visual = new DrawingVisual();

            //resize koefficient
            double kw = 1;

            if (SourceImage.PixelWidth > 0)
            {
                kw = (double)newPixelWidth / SourceImage.PixelWidth;
            }
            double kh = 1;

            if (SourceImage.PixelHeight > 0)
            {
                kh = (double)newPixelHeight / SourceImage.PixelHeight;
            }

            using (DrawingContext dc = visual.RenderOpen())
            {
                dc.DrawImage(SourceImage, new Rect(0, 0, SourceImage.PixelWidth, SourceImage.PixelHeight));

                foreach (Watermark w in Watermarks)
                {
                    //rotate drawing context
                    RotateTransform rt = new RotateTransform();
                    rt.Angle   = w.angle;
                    rt.CenterX = 0;
                    rt.CenterY = 0;
                    dc.PushTransform(rt);

                    //dc.PushOpacity(w.opacity);
                    double op = 1 - w.opacity / 100.0;
                    dc.PushOpacity(op);

                    TextWatermark tw = (TextWatermark)w;

                    int    x, y;
                    double sin, cos;
                    sin = Math.Sin(w.angle * Math.PI / 180.0);
                    cos = Math.Cos(w.angle * Math.PI / 180.0);
                    x   = Convert.ToInt16(tw.GetPixelXlocation(SourceImage.PixelWidth) * cos + tw.GetPixelYlocation(SourceImage.PixelHeight) * sin);
                    y   = Convert.ToInt16(-tw.GetPixelXlocation(SourceImage.PixelWidth) * sin + tw.GetPixelYlocation(SourceImage.PixelHeight) * cos);

                    dc.DrawText(tw.GetFormattedText(SourceImage.PixelWidth, SourceImage.PixelHeight), new Point(x, y));

                    dc.Pop(); //pop opacity
                    dc.Pop(); //pop rotation
                }
            }

            //resize transform
            visual.Transform = new ScaleTransform(kw, kh);

            RenderTargetBitmap rtb = new RenderTargetBitmap(newPixelWidth, newPixelHeight, 96, 96, PixelFormats.Pbgra32);

            rtb.Render(visual);

            return(rtb);
        }
Exemple #2
0
 private void AddAdornerToWatermarkLabel(TextWatermark watermark)
 {
     if (watermark != null)
     {
         AdornerLayer          layer = AdornerLayer.GetAdornerLayer(watermark.UiLabelOnImageInCanvas);
         WatermarkLabelAdorner a     = new WatermarkLabelAdorner(watermark.UiLabelOnImageInCanvas);
         a.OnDrag += TextWatermarkDragHandler;
         layer.Add(a);
     }
 }
Exemple #3
0
        private void buttonLoadTemplate_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName   = "MyWatermarks";
            dlg.DefaultExt = ".xml";
            dlg.Filter     = "Watermark templates (.xml)|*.xml";

            Nullable <bool> result = dlg.ShowDialog();

            if (result == true)
            {
                string        path   = dlg.FileName;
                XmlSerializer reader = new XmlSerializer(typeof(TextWatermarkSerializationHelper[]));
                TextWatermarkSerializationHelper[] th = null;
                try
                {
                    System.IO.StreamReader file = new System.IO.StreamReader(path);
                    th = (TextWatermarkSerializationHelper[])reader.Deserialize(file);
                }
                catch (Exception)
                { }
                if (th != null)
                {
                    RemoveAllAdornersFromLabelWatermarks();
                    textWatermarks = new TextWatermarkListWithSerchByUiLabel();
                    foreach (TextWatermarkSerializationHelper h in th)
                    {
                        System.Drawing.Color c = System.Drawing.Color.FromArgb(h.colorArgb);
                        TextWatermark        t = new TextWatermark(h.text,
                                                                   new System.Windows.Media.FontFamily(h.fontFamilyName),
                                                                   System.Windows.Media.Color.FromArgb(c.A, c.R, c.G, c.B),
                                                                   h.height,
                                                                   h.angle,
                                                                   h.opacity,
                                                                   h.x,
                                                                   h.y);

                        t.UiLabelOnImageInCanvas.MouseLeftButtonDown += WatermarkLabelOnCanvasTouched;
                        t.UiLabelOnImageInCanvas.Cursor = Cursors.Hand;
                        t.SetLabelGeometryAccordingToImageAndCanvas(imagePreview.Width, imagePreview.Height, canvasMain.ActualWidth, canvasMain.ActualHeight);
                        canvasMain.Children.Add(t.UiLabelOnImageInCanvas);

                        textWatermarks.Add(t);
                    }

                    AddAdornerToWatermarkLabel(textWatermarks[textWatermarks.Count - 1]);
                    tabItemTextWatermarks.DataContext = textWatermarks[textWatermarks.Count - 1];
                }
            }
        }
Exemple #4
0
        private void TextWatermarkDragHandler(WatermarkLabelAdorner sender, DragOperations operation, double diffX, double diffY)
        {
            Label l = sender.AdornedElement as Label;

            if (l == null)
            {
                return;
            }
            int i = textWatermarks.GetIndexByUiLabel(l);

            if (i < 0)
            {
                return;
            }
            TextWatermark t = textWatermarks[i];

            if (operation == DragOperations.Move)
            {
                int diffXinPercent = Convert.ToInt16((diffX / imagePreview.Width) * 100);
                int diffYinPercent = Convert.ToInt16((diffY / imagePreview.Height) * 100);
                t.xLocationInPercent += diffXinPercent;
                t.yLocationInPercent += diffYinPercent;
            }
            else
            if (operation == DragOperations.Rotate)
            {
                double ox = sender.ActualWidth * Math.Cos((t.angle) * Math.PI / 180);
                double oy = sender.ActualWidth * Math.Sin((t.angle) * Math.PI / 180);
                double nx = ox + diffX;
                if (nx < 0)
                {
                    if (diffY < 0)
                    {
                        t.angle = -90;
                    }
                    else
                    {
                        t.angle = 90;
                    }
                }
                else
                {
                    double ny = oy + diffY;
                    double na = Math.Atan(ny / nx) * (180.0 / Math.PI);
                    t.angle = Convert.ToInt16(na);
                }
            }
        }
Exemple #5
0
        private void AddNewWatermark()
        {
            RemoveAllAdornersFromLabelWatermarks();

            TextWatermark t = new TextWatermark((string)Application.Current.FindResource("newWatermarkText"),
                                                new System.Windows.Media.FontFamily("Arial"), Colors.Red, 10, 0, 70, 3, 3);

            //selection
            t.UiLabelOnImageInCanvas.MouseLeftButtonDown += WatermarkLabelOnCanvasTouched;
            t.UiLabelOnImageInCanvas.Cursor = Cursors.Hand;
            t.SetLabelGeometryAccordingToImageAndCanvas(imagePreview.Width, imagePreview.Height, canvasMain.ActualWidth, canvasMain.ActualHeight);
            canvasMain.Children.Add(t.UiLabelOnImageInCanvas);

            AddAdornerToWatermarkLabel(t);

            textWatermarks.Add(t);
            tabItemTextWatermarks.DataContext = t;
        }
Exemple #6
0
        private void buttonDeleteWatermark_Click(object sender, RoutedEventArgs e)
        {
            if (textWatermarks.Count > 0)
            {
                //remember selected watermark label
                TextWatermark foo = (TextWatermark)tabItemTextWatermarks.DataContext;

                int i = textWatermarks.GetIndexByUiLabel(foo.UiLabelOnImageInCanvas);
                if (i >= 0)
                {
                    tabItemTextWatermarks.DataContext = null;

                    canvasMain.Children.Remove(foo.UiLabelOnImageInCanvas);
                    textWatermarks.Remove(textWatermarks[i]);
                    if (textWatermarks.Count > 0)
                    {
                        tabItemTextWatermarks.DataContext = textWatermarks[textWatermarks.Count - 1];
                        AddAdornerToWatermarkLabel(textWatermarks[textWatermarks.Count - 1]);
                    }
                }
            }
        }