使用 FJCore 对 Silverlight WriteableBitmap 进行编码 [英] Using FJCore to encode Silverlight WriteableBitmap

查看:18
本文介绍了使用 FJCore 对 Silverlight WriteableBitmap 进行编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出如何使用 FJCore 将 WriteableBitmap 编码为 jpeg.我知道 WriteableBitmap 提供了原始像素,但我不确定如何将其转换为 FJCore 对其 JpegEncoder 方法所期望的格式.JpegEncoder 有两种重载,一种采用 FluxJpeg.Core.Image,另一种采用 DecodedJpeg.

I am trying to find out how to use FJCore to encode a WriteableBitmap to a jpeg. I understand that WriteableBitmap provides the raw pixels but I am not sure how to convert it to the format that FJCore expects for its JpegEncoder method. JpegEncoder has two overloads, one takes a FluxJpeg.Core.Image and the other takes in a DecodedJpeg.

我试图创建一个 FluxJpeg.Core.Image 但它需要一个 byte[][,] 图像数据.byte[n][x,y] 其中 x 是宽度,y 是高度,但我不知道 n 应该是什么.

I was trying to create a FluxJpeg.Core.Image but it expects a byte[][,] for the image data. byte[n][x,y] where x is width and y is height but I don't know what n should be.

我认为 n 应该是 4,因为它对应于每个像素中编码的 argb 信息,但是当我尝试 FJCore 时抛出一个参数超出范围异常.这是我尝试过的.Raster 是我的 byte[4][x,y] 数组.

I thought that n should be 4 since that would correspond to the argb info encoded in each pixel but when I tried that FJCore throws an argument out of range exception. Here is what I tried. Raster is my byte[4][x,y] array.

raster[0][x, y] = (byte)((pixel >> 24) & 0xFF);
raster[1][x, y] = (byte)((pixel >> 16) & 0xFF);
raster[2][x, y] = (byte)((pixel >> 8) & 0xFF);
raster[3][x, y] = (byte)(pixel & 0xFF);

推荐答案

想通了!我从 code.google.com 下载了 FJCore 并浏览了图像类.它只需要 RGB 字节.这是我写的函数.我需要图像的 base64 版本,这就是我的函数返回的内容.

Figured it out! I downloaded FJCore from code.google.com and went through the image class. It only expects the RGB bytes. Here is the function that I wrote. I need the base64 version of the image so that's what my function returns.

    private static string GetBase64Jpg(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, 90, 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;
    }

这篇关于使用 FJCore 对 Silverlight WriteableBitmap 进行编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆