使用FJCore编码Silverlight WriteableBitmap

发布于 2024-07-27 03:15:43 字数 656 浏览 5 评论 0原文

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

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

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

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);

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.

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.

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如痴如狂 2024-08-03 03:15:43

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

    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;
    }

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;
    }
指尖微凉心微凉 2024-08-03 03:15:43

这段代码很好,应该可以工作。 我使用相同的代码通过 Web 服务将图像流发送到服务器,然后使用这些字节重新生成图像...您也可以将这些字节保存到 Db

 [WebMethod]
 public string SaveImage(string data, string fileName)
 {
    byte[] imageBytes = System.Convert.FromBase64String(data);

    MemoryStream mem = new MemoryStream();
    mem.Write(imageBytes, 0, imageBytes.Length);

    System.Drawing.Image img = System.Drawing.Image.FromStream(mem);
    img.Save("D:\\FinalTest.jpg");      
    return "Saved !";
 }

This code is fine and it should work. I am using same code to send image stream to server via web service and than regenerate image using these bytes...you can save these bytes to Db also

 [WebMethod]
 public string SaveImage(string data, string fileName)
 {
    byte[] imageBytes = System.Convert.FromBase64String(data);

    MemoryStream mem = new MemoryStream();
    mem.Write(imageBytes, 0, imageBytes.Length);

    System.Drawing.Image img = System.Drawing.Image.FromStream(mem);
    img.Save("D:\\FinalTest.jpg");      
    return "Saved !";
 }
梦里人 2024-08-03 03:15:43

听起来 [n] 应该是图像的字节数组,我一直在研究将 WriteableBitmap 编码为 JPEG 并找到相同的库,但没有详细研究它,但假设情况是这样,将添加更多稍后再看这个答案,看看这是否有效,因为我还没有机会尝试。 我猜想在 Silverlight 中会有一些方法来获取 WritableBitmap 的字节,因为可以保存为其他类型。

Sounds like [n] should be the byte-array of the image, I have been looking into encoding WriteableBitmap into a JPEG and found the same library but have not looked into it in detail, but assume this would be the case, will add more later to this answer to see if this works, as I have not have the chance to try it yet. There will be some method to get the bytes of a WritableBitmap in Silverlight I guess as it is possible to save to other types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文