2

I'm here for a solution to a little unresolved problem. Maybe I haven't searched for the right things as I haven't found a solution and I hope you will be able to help.

So I have a WPF application where I load an image, here I have no problems. Next I have to add some points by clicking, so i have this function:

 private void ClickMouse(object sender, MouseButtonEventArgs e)
    {
        Point p = e.GetPosition(this);
        schema.PointFromScreen(p);
        var myImage3 = new Image();
        var bi3 = new BitmapImage();
        bi3.BeginInit();
        bi3.UriSource = new Uri(_clickImagePath, UriKind.Relative);
        bi3.EndInit();
        myImage3.Stretch = Stretch.Fill;
        myImage3.Source = bi3;
        myImage3.MinWidth = 30;
        myImage3.MinHeight = 30;
        myImage3.Width = 30;
        myImage3.Height = 30;
        myImage3.Name = _pointName;
        var oMargin = new Thickness(p.X, p.Y, 0, 0);
        myImage3.Margin = oMargin;
        mainCanva.Children.Add(myImage3);
        _listImg.Add(myImage3);
        PointList.Items.Add(_pointName);
    }

The problem is not here but I need a context.

Now on to the main problem. I have to save the final Image to make a PDF with. I use a piece of sample code I found here (http://stackoverflow.com/questions/1824989/how-to-store-system-windows-controls-image-to-local-disk) to convert to a jpg file using ITextsharp. The only thing I have on the PDF is the first picture, so I admitted that it probably take only the first image and not the other created images on it but I really don't know how to fix it. Does some fonction exist to make a kind of screenshot of the image zone? or maybe to pay attention to all image in the zone of the principal Control.Image ?

Thank you for your help.

CSharpened
  • 11,674
  • 14
  • 52
  • 86

1 Answers1

1

You should use the following code to render your Canvas as an image :

var rect = new Rect(canvas.RenderSize);
var rtb = new RenderTargetBitmap((int)rect.Right, (int)rect.Bottom, 96d, 96d, System.Windows.Media.PixelFormats.Default);
rtb.Render(canvas);
//encode as PNG
varpngEncoder = new PngBitmapEncoder();
pngEncoder.Frames.Add(BitmapFrame.Create(rtb));

//save to memory stream
using(var ms = new System.IO.MemoryStream())
{
    pngEncoder.Save(ms);

    System.IO.File.WriteAllBytes("logo.png", ms.ToArray());
}

(Source : Write WPF output to image file )

Community
  • 1
  • 1
mathieu
  • 30,974
  • 4
  • 64
  • 90