开发者

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?开发者_如何转开发


Use WriteableBitmap class.

References:

  • Rendering XAML to a JPEG using Silverlight 3
  • Silverlight 3.0: WriteableBitmap
  • Silverlight 3's New Writeable Bitmap

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.


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.


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜