curiosity
curiosity

Reputation: 1233

silverlight draw on bitmap

Image img = new Bitmap(Image.FromFile(file.FullName));

using (Graphics g = Graphics.FromImage(img)){
    g.DrawRectangle(Pens.Black, 0, 0, img.Width - 2, img.Height - 2);
}

like this

how to do in sliverlight?

Upvotes: 0

Views: 3763

Answers (3)

decyclone
decyclone

Reputation: 30830

Use WriteableBitmap class.

References:

Example:

With WritableBitmap, you can just draw something on a Control or Canvas and then save it to a bitmap using it's public WriteableBitmap(UIElement element,Transform transform) constructor.

Upvotes: 2

Rene Schulte
Rene Schulte

Reputation: 2962

You don't need to render a SL Rectangle into the WriteableBitmap. The WriteableBitmapEx open source library is perfect for this. See the project site for an example of the DrawRectangle method. http://writeablebitmapex.codeplex.com

There are also live samples, including the Shape sample.

You can also find the code of the samples in the source code repository.

Upvotes: 2

Chris Taylor
Chris Taylor

Reputation: 53699

You can use a WriteableBitmap for this. Create a Canvas and draw your elements on the Canvas, load other images etc. Then once you are done rendering on the Canvase you can create the WriteableBitmap from the Canvas and then do what every you need.

In the example below I assigned the bitmap as the Source of an Image element to show that the final result.

Canvas canvas = new Canvas();
UIElement ellipse = new Ellipse() 
  { Width = 100, Height = 100, Fill = new SolidColorBrush(Colors.Red) };
Canvas.SetLeft(ellipse, 100);
Canvas.SetTop(ellipse, 100);
canvas.Children.Add(ellipse);

WriteableBitmap bmp = new WriteableBitmap(canvas, null);
myImage.Source = bmp;

Upvotes: 1

Related Questions