none
如何將DrawingImage保存為圖片? RRS feed

  • 问题

  • 比如說,我用DrawingImage繪制了一幅圖,并把它呈現在image控件里.
    LineGeometry line = new LineGeometry(new Point(0, 0), new Point(100, 100));
    DrawingImage drawing=new DrawingImage(line);//實際代碼中的drawing是個非常復雜的DrawingGroup
    image.Source=drawing;//image是一個Image控件
    現在我想把這幅圖保存為圖像文件,應該怎么辦?
    2009年5月23日 16:21

答案

  • 您好,

     

    Killmyday在上面提供了一个很好的贴子,下面是一个可以直接运行的小例了:

    XAML code:

     

        <StackPanel>

            <Image Stretch="None" Name="image" Width="100" Height="100">

                <Image.Source>

                    <DrawingImage>

                        <DrawingImage.Drawing>

                            <GeometryDrawing Brush="Blue">

                                <GeometryDrawing.Pen>

                                    <Pen Brush="Red" Thickness="5" />

                                </GeometryDrawing.Pen>

                                <GeometryDrawing.Geometry>

                                    <EllipseGeometry  Center="0,0" RadiusX="50" RadiusY="50" />

                                </GeometryDrawing.Geometry>

                            </GeometryDrawing>

                        </DrawingImage.Drawing>

                    </DrawingImage>

                </Image.Source>

            </Image >

            <Button Content="Save" Click="OnSave" Width="120" Height="30" />

    </StackPanel>

     

    后台代码:
       private void OnSave(object sender, RoutedEventArgs e)

            {

                Uri path = new Uri(@"C:/test.png", UriKind.Absolute);

                ExportToPng(path, image);

            }

            public void ExportToPng(Uri path, Image  canvas)

            {

                if (path == null) return;

                // Save current canvas transform

                Transform transform = canvas.LayoutTransform;

                // reset current transform (in case it is scaled or rotated)

                canvas.LayoutTransform = null;

                // Get the size of canvas

                Size size = new Size(canvas.Width, canvas.Height);

                // Measure and arrange the canvas

                // VERY IMPORTANT

                canvas.Measure(size);

                canvas.Arrange(new Rect(size));

                // Create a render bitmap and push the canvas to it

                RenderTargetBitmap renderBitmap =

                  new RenderTargetBitmap(

                    (int)size.Width,

                    (int)size.Height,

                    96d,

                    96d,

                    PixelFormats.Pbgra32);

                renderBitmap.Render(canvas);

                // Create a file stream for saving image

                using (FileStream outStream = new FileStream(path.LocalPath, FileMode.Create))

                {

                    // Use png encoder for our data

                    PngBitmapEncoder encoder = new PngBitmapEncoder();

                    // push the rendered bitmap to it

                    encoder.Frames.Add(BitmapFrame.Create(renderBitmap));

                    // save the data to the stream

                    encoder.Save(outStream);

                }

                // Restore previously saved layout

                canvas.LayoutTransform = transform;

            }

     

    注:ExportToPng方法引用自下面这个link:

    http://dvuyka.spaces.live.com/blog/cns!305B02907E9BE19A!240.entry

     

    谢谢


    Jim Zhou -MSFT
    2009年5月27日 6:39
    版主

全部回复

  • 您好,

     

    Killmyday在上面提供了一个很好的贴子,下面是一个可以直接运行的小例了:

    XAML code:

     

        <StackPanel>

            <Image Stretch="None" Name="image" Width="100" Height="100">

                <Image.Source>

                    <DrawingImage>

                        <DrawingImage.Drawing>

                            <GeometryDrawing Brush="Blue">

                                <GeometryDrawing.Pen>

                                    <Pen Brush="Red" Thickness="5" />

                                </GeometryDrawing.Pen>

                                <GeometryDrawing.Geometry>

                                    <EllipseGeometry  Center="0,0" RadiusX="50" RadiusY="50" />

                                </GeometryDrawing.Geometry>

                            </GeometryDrawing>

                        </DrawingImage.Drawing>

                    </DrawingImage>

                </Image.Source>

            </Image >

            <Button Content="Save" Click="OnSave" Width="120" Height="30" />

    </StackPanel>

     

    后台代码:
       private void OnSave(object sender, RoutedEventArgs e)

            {

                Uri path = new Uri(@"C:/test.png", UriKind.Absolute);

                ExportToPng(path, image);

            }

            public void ExportToPng(Uri path, Image  canvas)

            {

                if (path == null) return;

                // Save current canvas transform

                Transform transform = canvas.LayoutTransform;

                // reset current transform (in case it is scaled or rotated)

                canvas.LayoutTransform = null;

                // Get the size of canvas

                Size size = new Size(canvas.Width, canvas.Height);

                // Measure and arrange the canvas

                // VERY IMPORTANT

                canvas.Measure(size);

                canvas.Arrange(new Rect(size));

                // Create a render bitmap and push the canvas to it

                RenderTargetBitmap renderBitmap =

                  new RenderTargetBitmap(

                    (int)size.Width,

                    (int)size.Height,

                    96d,

                    96d,

                    PixelFormats.Pbgra32);

                renderBitmap.Render(canvas);

                // Create a file stream for saving image

                using (FileStream outStream = new FileStream(path.LocalPath, FileMode.Create))

                {

                    // Use png encoder for our data

                    PngBitmapEncoder encoder = new PngBitmapEncoder();

                    // push the rendered bitmap to it

                    encoder.Frames.Add(BitmapFrame.Create(renderBitmap));

                    // save the data to the stream

                    encoder.Save(outStream);

                }

                // Restore previously saved layout

                canvas.LayoutTransform = transform;

            }

     

    注:ExportToPng方法引用自下面这个link:

    http://dvuyka.spaces.live.com/blog/cns!305B02907E9BE19A!240.entry

     

    谢谢


    Jim Zhou -MSFT
    2009年5月27日 6:39
    版主