在 Silverlight 中,有一个包含线条、矩形、图像的画布。我想从画布中获取图像流。是否可以?
问问题
607 次
1 回答
2
您可以从https://code.google.com/p/fjcore/获取开源 FJCore 代码。将 FJCore 代码导入您的 silverlight 应用程序,然后使用下面的代码。
WriteableBitmap wb = new WriteableBitmap(this.canvas1,null);
Stream sm = GetBase64Image(wb);
public static Stream GetBase64Image(WriteableBitmap bitmap)
{
int width = bitmap.PixelWidth;
int height = bitmap.PixelHeight;
int bands = 3;
byte[][,] raster = new byte[bands][,];
for (int i = 0; i < bands; i++)
{
raster[i] = new byte[width, height];
}
for (int row = 0; row < height; row++)
{
for (int column = 0; column < width; column++)
{
int pixel = bitmap.Pixels[width * row + column];
raster[0][column, row] = (byte)(pixel >> 16);
raster[1][column, row] = (byte)(pixel >> 8);
raster[2][column, row] = (byte)pixel;
}
}
ColorModel model = new ColorModel { colorspace = ColorSpace.RGB };
FluxJpeg.Core.Image img = new FluxJpeg.Core.Image(model, raster);
MemoryStream stream = new MemoryStream();
JpegEncoder encoder = new JpegEncoder(img, 100, stream);
encoder.Encode();
return stream;
}
于 2013-05-16T08:57:30.913 回答