如何从画布中捕获整个图像?

时间:2016-10-29 19:45:14

标签: wpf image-processing canvas save

我正在开发一个项目来对图像上的要素进行编号。被编号的每个项目是打印在画布顶部上的文本块对象,其具有使用图像画笔绘制到其背景的jpg(或bmp / gif / png)。 当需要保存图形时,我希望有两个选项,一个用于将其保存为新的jpg(或其他格式),其中文本块对象呈现在其中。或选项b,将这些文本块的位置保存到单独的文件(XML,XAML,你有什么),这样你就可以回来继续编辑条目。

以下是我的一些代码 这是我试图保存图像的地方

//Global members   
Image i;    
static Size _Size = new Size();   


 private void miLoadImage_Click(object sender, RoutedEventArgs e)
        {
            // Create OpenFileDialog 
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();


            // Set filter for file extension and default file extension 
            dlg.DefaultExt = ".png";
            dlg.Filter = @"All supported graphics|*.jpg;*.jpeg;*.png;*.gif;*.bmp|
                        JPEG Files (*.jpeg)|*.jpeg|
                        PNG Files (*.png)|*.png|
                        JPG Files (*.jpg)|*.jpg|
                        GIF Files (*.gif)|*.gif|
                        BMP Files (*.bmp)|*.bmp";
            // Display OpenFileDialog by calling ShowDialog method 
            Nullable<bool> result = dlg.ShowDialog();

            // Get the selected file name and display in a TextBox 
            if (result == true)
            {
                // Open document 
                string filename = dlg.FileName;
                Uri uri = new Uri(@dlg.FileName, UriKind.Relative);

                //Use canvas rather than image
                ImageBrush ib = new ImageBrush();
                ib.ImageSource = new BitmapImage(new Uri(filename, UriKind.Relative));
                canvas.Background = ib;

                if (i == null)
                    i = new Image();
                i.Source = new BitmapImage(new Uri(dlg.FileName));
                _Size = new Size(i.Source.Width, i.Source.Height);

            }
        }

这是我用来加载图片的代码

 public void WriteTextToImage(Point position)
    {
        SolidColorBrush brush = new SolidColorBrush((Color)cpColor.SelectedColor);
        //Get something to write on (not an old receipt...)
        TextBlock textBlock = new TextBlock();
        //Say something useful... well something atleast... 
        textBlock.Text = tbCurrentLabel.Text;
        textBlock.FontSize = slFontSize.Value;
        textBlock.Foreground = brush;
        Canvas.SetLeft(textBlock, position.X);
        Canvas.SetTop(textBlock, position.Y);
        canvas.Children.Add(textBlock);

        //Need to update the canvas, was not seeing children before doing this.
        canvas.UpdateLayout();

        //Iterate the label text
        IterateLabel();
    }

这是我用来将文字写入图像的代码。

<IfModule mod_rewrite.c>
   RewriteEngine on  
   RewriteCond %{REQUEST_FILENAME} -s [OR]
   RewriteCond %{REQUEST_FILENAME} -l [OR]
   RewriteCond %{REQUEST_FILENAME} -d
   RewriteRule ^.*$ - [NC,L]
   #RewriteRule ^(.*) /index.html [NC,L]
   RewriteRule ^ index.html [L]
</IfModule>

现在我只获得了当前在画布中可见的图像的一个角落,并且正在被裁剪,通过摆弄DPI和_Size值我可以改变显示的图像数量,但它对于那个图像来说是静态的(如果我加载一个不同的图像,那么所有的值都是错误的,而且我只得到图像的一小部分)

如果有人能帮我把头拉出来(咳咳),我将非常感激! TIA

1 个答案:

答案 0 :(得分:0)

保存整个Canvas

        RenderTargetBitmap bmp = new RenderTargetBitmap((int)Cnv.ActualWidth, (int)Cnv.ActualHeight, 100.0, 100.0, PixelFormats.Default);
        bmp.Render(Cnv);                       

        PngBitmapEncoder encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(bmp));

        System.IO.FileStream stream = System.IO.File.Create("G:\\Canvas.png");
        encoder.Save(stream);
        stream.Close();

仅保存背景

    BitmapFrame bmp = (BitmapFrame)(Cnv.Background as ImageBrush).ImageSource;

    PngBitmapEncoder encoder = new PngBitmapEncoder();
    encoder.Frames.Add(bmp);

    System.IO.FileStream stream = System.IO.File.Create("G:\\Canvas.png");
    encoder.Save(stream);
    stream.Close();
相关问题