locked
Save draw images xml with .jpg,.png and etc RRS feed

  • Question

  •  draw images in drawing canvas panel to save .jpg, .png and etc  save this images are not open in photoshop software anybody help me..
    Tuesday, August 27, 2013 9:05 AM

Answers

  • Hi,

    According to your description, i think you want to save the image which you are drawing in canvas to multiple formats.

    There are several image encoders available, please check the reference below:

    #System.Windows.Media.Imaging Namespace
    http://msdn.microsoft.com/en-us/library/ms608877(v=vs.110).aspx

    In my sample, i can draw on the canvas, after clicking the button, it can be saved as jpg and png.

    public static void SaveWindow(Window window, int dpi, string filename)
    {
    
                var rtb = new RenderTargetBitmap(
                    (int)window.Width, //width 
                    (int)window.Width, //height 
                    dpi, //dpi x 
                    dpi, //dpi y 
                    PixelFormats.Pbgra32 // pixelformat 
                    );
                rtb.Render(window);
    
                SaveRTBAsPNG(rtb, filename);
                SaveRTBAsJPG(rtb, filename);
    }
    
    public static void SaveCanvas(Window window, Canvas canvas, int dpi, string filename)
    {
                Size size = new Size(window.Width, window.Height);
                canvas.Measure(size);
                //canvas.Arrange(new Rect(size));
    
                var rtb = new RenderTargetBitmap(
                    (int)window.Width, //width 
                    (int)window.Height, //height 
                    dpi, //dpi x 
                    dpi, //dpi y 
                    PixelFormats.Pbgra32 // pixelformat 
                    );
                rtb.Render(canvas);
    
                SaveRTBAsPNG(rtb, filename);
                SaveRTBAsJPG(rtb, filename);
    }
    
    private static void SaveRTBAsPNG(RenderTargetBitmap bmp, string filename)
    {
                filename += ".png";
                var enc = new System.Windows.Media.Imaging.PngBitmapEncoder();
                enc.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bmp));
    
                using (var stm = System.IO.File.Create(filename))
                {
                    enc.Save(stm);
                }
    }
    
    private static void SaveRTBAsJPG(RenderTargetBitmap bmp, string filename)
    {
                filename += ".jpg";
                var enc = new System.Windows.Media.Imaging.JpegBitmapEncoder();
                enc.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bmp));
    
                using (var stm = System.IO.File.Create(filename))
                {
                    enc.Save(stm);
                }
    }

    I've uploaded it to SkyDrive, you can download it:
    http://sdrv.ms/13XQeUb


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, August 28, 2013 2:25 AM