9

Saving multiple canvas controls in image under Silverlight / wpf

 2 years ago
source link: https://www.codesd.com/item/saving-multiple-canvas-controls-in-image-under-silverlight-wpf.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Saving multiple canvas controls in image under Silverlight / wpf

advertisements

I have a situation where I have a canvas with an image and I lay multiple canvas controls over it and each canvas may have multiple controls drawn on them (Ellipses, Lines, etc). What is the best/most efficient way to save all these canvas controls toone image? I assume WritableBitmap, but how will this work with multiple canvas controls? One given is that each canvas will be the same size.

EDIT:

This is what I tried. I also tried with the dpi of the original image, which gives me the tiny result.

BitmapSource bms = (BitmapSource)this.ctlImage.Source;

Matrix m = PresentationSource.FromVisual(this.ctlImgCanvas).CompositionTarget.TransformToDevice; 

RenderTargetBitmap rtb = new RenderTargetBitmap(bms.PixelWidth, bms.PixelHeight, m.M11 * 96.0, m.M22 * 96.0, PixelFormats.Pbgra32);

rtb.Render(this.ctlImgCanvas);

PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtb));
            encoder.Save(memStream);


This will take care of everything including children of the canvas. You don't need to know the size of the canvas in advance.

Matrix m =
PresentationSource.FromVisual(canvas).CompositionTarget.TransformToDevice;

BitmapSource panelBitmap = new RenderTargetBitmap(
                (int)Math.Floor(canvas.ActualWidth),
                (int)Math.Floor(canvas.ActualHeight),
                m.M11 * 96.0, // For DPI. This hardcoded value is correct. Check WPF DPI
                m.M22 * 96.0,
                PixelFormats.Default);

panelBitmap.Render(canvas);

using (FileStream fs = File.Open([filename], FileModel.Create, FileAccess.Write))
{
    BitmapEncoder encoder = GetBitmapEncoderForFileExtension([filename]); // See documentation for which encoder to use when.
    encoder.Frames.Add(BitmapFrame.Create(panelBitmap ));
    encoder.Save(fs);
}


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK