silverlight页面保存为图片

自己整理了一下 直接上代码:
private void button1_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog saveDlg = new SaveFileDialog();
            saveDlg.Filter = "jpg file format|*.jpg";
            saveDlg.DefaultExt = ".jpeg";
            if ((bool)saveDlg.ShowDialog())
            {
                Stream fs = saveDlg.OpenFile();
                WriteableBitmap wb = new WriteableBitmap(LayoutRoot, null);
                byte[] b = Convert.FromBase64String(GetBase64Image(wb));
                fs.Write(b, 0, b.Length);
                fs.Flush();
                fs.Close();
            }
        }
        /// <summary>
        /// 老外写的方法:将WriteableBitmap转化为base64位字符串
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        private string 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();

            stream.Seek(0, SeekOrigin.Begin);
            byte[] binaryData = new Byte[stream.Length];
            long bytesRead = stream.Read(binaryData, 0, (int)stream.Length);

            string base64String =
                    System.Convert.ToBase64String(binaryData,
                                                  0,
                                                  binaryData.Length);

            return base64String;
        }

方法二:

public class ImageHepler
    {
        /// <summary>
        /// 截图
        /// </summary>
        /// <param name="element">被截图的对象</param>
        /// <param name="transform">等比缩放</param>
        /// <returns></returns>
        public static byte[] GetElementImage(FrameworkElement element, Transform transform)
        {
            byte[] buffer = null;
            WriteableBitmap bitmap = new WriteableBitmap(element, transform);
            if (bitmap != null)
            {
                MemoryStream imageStream = GetImageStream(bitmap);
                buffer = new byte[imageStream.Length];
                long num = imageStream.Read(buffer, 0, (int)imageStream.Length);
            }
            return buffer;
        }
        /// <summary>
        /// 图片内存流
        /// </summary>
        /// <param name="element">被截图的对象</param>
        /// <param name="transform">等比缩放</param>
        /// <returns></returns>
        public static MemoryStream GetElementImageStream(FrameworkElement element, Transform transform)
        {
            WriteableBitmap bitmap = new WriteableBitmap(element, transform);
            if (bitmap != null)
            {
                MemoryStream imageStream = GetImageStream(bitmap);
                return imageStream;
            }
            return null;

        }
        private static MemoryStream GetImageStream(WriteableBitmap bitmap)
        {
            return EncodeRasterInformationToStream(ReadRasterInformation(bitmap), ColorSpace.RGB);
        }
        private static MemoryStream EncodeRasterInformationToStream(byte[][,] raster, ColorSpace colorSpace)
        {
            ColorModel model2 = new ColorModel();
            model2.colorspace = colorSpace;
            ColorModel model = model2;
            FluxJpeg.Core.Image image = new FluxJpeg.Core.Image(model, raster);
            MemoryStream stream = new MemoryStream();
            // new JpegEncoder(image, 100, stream).Encode();
            FluxJpeg.Core.Encoder.JpegEncoder encoder = new FluxJpeg.Core.Encoder.JpegEncoder(image, 100, stream);
            encoder.Encode();
            stream.Seek(0L, SeekOrigin.Begin);
            return stream;
        }
        private static byte[][,] ReadRasterInformation(WriteableBitmap bitmap)
        {
            int num = bitmap.PixelWidth;
            int num2 = bitmap.PixelHeight;
            int num3 = 3;
            byte[][,] bufferArray = new byte[num3][,];
            for (int i = 0; i < num3; i++)
            {
                bufferArray[i] = new byte[num, num2];
            }
            for (int j = 0; j < num2; j++)
            {
                for (int k = 0; k < num; k++)
                {
                    int num7 = bitmap.Pixels[(num * j) + k];
                    bufferArray[0][k, j] = (byte)(num7 >> 0x10);
                    bufferArray[1][k, j] = (byte)(num7 >> 8);
                    bufferArray[2][k, j] = (byte)num7;
                }
            }
            return bufferArray;
        }
    }
private void button1_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog saveDlg = new SaveFileDialog();
            saveDlg.Filter = "jpg file format|*.jpg";
            saveDlg.DefaultExt = ".jpeg";
            if ((bool)saveDlg.ShowDialog())
            {
                Stream fs = saveDlg.OpenFile();
                byte[] by = ImageHepler.GetElementImage(LayoutRoot, new ScaleTransform() { ScaleX = 1, ScaleY = 1 });
                fs.Write(by, 0, by.Length);
                fs.Flush();
                fs.Close();
            }
        }
dll下载:
FJ.Core.dll
silverPDF.dll
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值