示例#1
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];
                }
            }
        }
示例#2
0
文件: Watermarking.cs 项目: zd333/biw
        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);
        }
示例#3
0
文件: Watermarking.cs 项目: zd333/biw
        public static bool WatermarkScaleAndSaveImageFromBitmapImage(ImageFiletypes fileFormat, BitmapImage sourceImage, int newPixelWidth, int newPixelHight, TextWatermarkListWithSerchByUiLabel watermarks, string saveDirectoryPath, string fileNameWithoutExtensionAndPath)
        {
            RenderTargetBitmap rtb = CreateWatermarkedBitmapEncoder(sourceImage, watermarks, newPixelWidth, newPixelHight);

            return(SaveImage(rtb, fileFormat, saveDirectoryPath, fileNameWithoutExtensionAndPath));
        }