Graphics from windows forms to Silverlight?
The following code works for windows forms. How can I do this to Silverlight? I guess there is no graphics object.
Bitmap bm = new Bitmap(600, 600);
Graphics g = Graphics.FromImage(bm);
Brush b = new LinearGradientB开发者_StackOverflow中文版rush(
new Point(1, 1), new Point(600, 600),
Color.White, Color.Red);
Point[] points = new Point[]
{new Point(10, 10),
new Point(77, 500),
new Point(590, 100),
new Point(250, 590),
new Point(300, 410)};
g.FillPolygon(b, points);
bm.Save("testandoImagem.jpg", ImageFormat.Jpeg);
Alan, even from a SL client application you can do this all server side, you are not showing anything in the User Interface, I would put this code in the business layer which has full access to the .NET Framework. Keep in mind that from SL you can't save on the client machine directly so you should use another way.
You could render the shape as a WriteableBitmap, but Silverlight does not have built in support for encoding the data to JPEG.
See these StackOverflow questions:
How to save BitmapImage / WriteableBitmap using SaveFileDialog in Silverlight 3.0?
Best Jpeg Encoder for Silverlight 4.0
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.writeablebitmap.render(v=VS.95).aspx
WriteableBitmap wb = new WriteableBitmap(600, 600);
Polygon p = new Polygon();
p.Points = new PointCollection() { new Point(10, 10), new Point(77, 500), new Point(590, 100), new Point(250, 590), new Point(300, 410) };
p.Fill = new LinearGradientBrush()
{
//Gradient angle is 0,0 to 1,1 by default
GradientStops = new GradientStopCollection() {
new GradientStop() { Color = Colors.White, Offset = 0 },
new GradientStop() { Color = Colors.Red, Offset = 1 } }
};
wb.Render(p, null);
wb.Invalidate();
//Save WriteableBitmap as described in other questions
精彩评论