
求一段Silverlight截图 的代码
1个回答
展开全部
图片的截取和压缩主要通过WriteableBitmap 这个类实现的,对它进行相应的变换就可以实现截取和压缩。
由WriteableBitmap转换成byte我调用了FluxJpeg这个三方的。博客园上有很多说明,在博客园上搜索“FluxJpeg”就可以找的到,
也有很详细说明。
还有一个我用到的类:
public class ImgByte
{
public static byte[] BitMapToByte(System.Windows.Media.Imaging.WriteableBitmap bitmap)
{
if (bitmap == null) return null;
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];
byte a = ((byte)(pixel >> 24));
byte r = (byte)(pixel >> 16);//4 R
byte g = (byte)(pixel >> 8);//2 G
byte b = (byte)pixel;//0 B
if (a < 2)
{
raster[0][column, row] = (byte)(255 - r);
raster[1][column, row] = (byte)(255 - g);
raster[2][column, row] = (byte)(255 - b);
}
else
{
raster[0][column, row] = (byte)(r * 255.0 / a);
raster[1][column, row] = (byte)(g * 255.0 / a);
raster[2][column, row] = (byte)(b * 255.0 / a);
}
}
}
FluxJpeg.Core.ColorModel model = new FluxJpeg.Core.ColorModel { colorspace = FluxJpeg.Core.ColorSpace.RGB };
FluxJpeg.Core.Image img = new FluxJpeg.Core.Image(model, raster);
//Encode the Image as a JPEG
System.IO.MemoryStream stream = new System.IO.MemoryStream();
FluxJpeg.Core.Encoder.JpegEncoder encoder = new FluxJpeg.Core.Encoder.JpegEncoder(img, 100, stream);
encoder.Encode();
//Back to the start
stream.Seek(0, System.IO.SeekOrigin.Begin);
//Get teh Bytes and write them to the stream
byte[] binaryData = new byte[stream.Length];
long bytesRead = stream.Read(binaryData, 0, (int)stream.Length);
return binaryData;
}
}
由WriteableBitmap转换成byte我调用了FluxJpeg这个三方的。博客园上有很多说明,在博客园上搜索“FluxJpeg”就可以找的到,
也有很详细说明。
还有一个我用到的类:
public class ImgByte
{
public static byte[] BitMapToByte(System.Windows.Media.Imaging.WriteableBitmap bitmap)
{
if (bitmap == null) return null;
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];
byte a = ((byte)(pixel >> 24));
byte r = (byte)(pixel >> 16);//4 R
byte g = (byte)(pixel >> 8);//2 G
byte b = (byte)pixel;//0 B
if (a < 2)
{
raster[0][column, row] = (byte)(255 - r);
raster[1][column, row] = (byte)(255 - g);
raster[2][column, row] = (byte)(255 - b);
}
else
{
raster[0][column, row] = (byte)(r * 255.0 / a);
raster[1][column, row] = (byte)(g * 255.0 / a);
raster[2][column, row] = (byte)(b * 255.0 / a);
}
}
}
FluxJpeg.Core.ColorModel model = new FluxJpeg.Core.ColorModel { colorspace = FluxJpeg.Core.ColorSpace.RGB };
FluxJpeg.Core.Image img = new FluxJpeg.Core.Image(model, raster);
//Encode the Image as a JPEG
System.IO.MemoryStream stream = new System.IO.MemoryStream();
FluxJpeg.Core.Encoder.JpegEncoder encoder = new FluxJpeg.Core.Encoder.JpegEncoder(img, 100, stream);
encoder.Encode();
//Back to the start
stream.Seek(0, System.IO.SeekOrigin.Begin);
//Get teh Bytes and write them to the stream
byte[] binaryData = new byte[stream.Length];
long bytesRead = stream.Read(binaryData, 0, (int)stream.Length);
return binaryData;
}
}

2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图...
点击进入详情页
本回答由AiPPT提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询