-1

I am trying to save a byte array (of an image) to the file system using C# and it isn't working using either method I have tried, here is code sample 1:

string path = @"c:\ppd" + g.ToString() + ".jpg";
        using (MemoryStream inputStream = new MemoryStream(image))
        {
            Image returnImage = Image.FromStream(inputStream);
            returnImage.Save(path, ImageFormat.Jpeg);
        }

With this code I get 'Parameter is not valid' when sending the data to the service. So that's obviously the wrong code for the array, so I hit google and came up with this code:

string path = @"c:\ppd\" + g.ToString() + ".jpg";
        using (MemoryStream inputStream = new MemoryStream(image))
        {
            using (Stream file = File.Create(path))
            {
                byte[] buffer = new byte[8 * 1024];
                int len;
                while ((len = inputStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    file.Write(buffer, 0, len);
                }
            }
        }

This code writes the file to the file system, but when you try to open it it helpfully says 'This is not a valid bitmap file, or it's format is not currently supported.

We get the byteArray from Flex using:

bitmapData.getPixels(0, 0, bitmapData.width, bitmapData.height);

And the bitmapData comes from the Android camera (via the Distriqt Camera ANE). The image displays fine in the app, but I have no idea where to go next with this, please help.

EDIT: The code in Flex to get the byteArray is:

<s:BitmapImage id="bitmapImage" width="100%"
                 maxHeight="{this.height/2}" 
                 source="{profileModel.captureBitmapPreview}"
                 />

<s:Button click="{dispatchEvent(new PictureEvent
                      (PictureEvent.UPLOAD,
                      0, pictureModel.captureBitmapPreview.getPixels(new Rectangle(0,0,pictureModel.captureBitmapPreview.width,pictureModel.captureBitmapPreview.height))))}"/>
/* this is the alternative which still doesn't work **/
<s:Button click="{dispatchEvent(new PictureEvent
                      (PictureEvent.UPLOAD,
                      0, bitmapImage.getPixels(new Rectangle(0,0,bitmapImage.width,bitmapImage.height))))}"/>

PictureEvent takes 3 parameters, type:String, id:int = 0, image:ByteArray = null.

SECOND EDIT: Here is the code that creates the bitmapData:

private function camera_capturedImageHandler(evt:CameraDataEvent):void
    {
        Camera.instance.setPresetMode(CameraMode.PRESET_MEDIUM);
        Camera.instance.addEventListener(CameraEvent.VIDEO_FRAME, camera_videoFrameHandler, false, 0, true);

        if (_captureBitmapData.width != evt.data.width || _captureBitmapData.height != evt.data.height)
        {
            _captureBitmapData = new BitmapData(evt.data.width, evt.data.height, false);
        }
        _captureBitmapData.draw(evt.data);
        var tbd:BitmapData = new BitmapData(FlexGlobals.topLevelApplication.width, FlexGlobals.topLevelApplication.height, false)
        tbd = applyOrientation(_bitmapData, "6");
        profileModel.captureBitmapPreview.copyPixels(tbd, new Rectangle(0, ((FlexGlobals.topLevelApplication.height/2)-(FlexGlobals.topLevelApplication.height/4)), FlexGlobals.topLevelApplication.width, FlexGlobals.topLevelApplication.height/2), new Point(0, 0));
    }

THIRD EDIT: I has done some testing and discovered that this code should work, it is a variation on the first block above that throws 'Parameter is not valid'

ImageConverter imageConverter = new ImageConverter();
        Bitmap bm = (Bitmap)imageConverter.ConvertFrom(image);

        if (bm != null && (bm.HorizontalResolution != (int)bm.HorizontalResolution || bm.VerticalResolution != (int)bm.VerticalResolution))
        {
            bm.SetResolution((int)(bm.HorizontalResolution + 0.5f),(int)(bm.VerticalResolution + 0.5f));
        }

        bm.Save(path, ImageFormat.Jpeg);

So now I am sure the error is with the byteArray, but I genuinely cannot see what I am doing wrong. I have the bitmapData and it is valid, and it looks like I am collecting the data I need, the written file is nearly 700Kb in size, but something is wrong. If someone could tell me what is wrong on the Flex side of this equation I would be forever grateful. I have the BitmapImage on the display list, it is bound to the model correctly and I have tried different ways of pointing to the source and drawing up the rectangle.

4

1 回答 1

0

好的,所以@Lorek 是对的,byteArray 只包含像素数据,没有格式信息。我们不知道为什么会这样,但基于这些知识和对数据库的一点挖掘,我意识到这种情况已经有一段时间了,我需要一个可以解决它的解决方案。因此,在回答“如何将像素数据从 byteArray 转换为图像?”这个问题时 我想出了这个,仍然需要一点帮助,但基本上这解决了我遇到的问题(代码后更多)

Bitmap bitmap = new Bitmap(150, 150, PixelFormat.Format32bppRgb);
BitmapData bmData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
IntPtr pNative = bmData.Scan0;
Marshal.Copy(image, 0, pNative, image.Length);
bitmap.UnlockBits(bmData);
bitmap.Save(path, ImageFormat.Png);

所以我在 Flex 中更改了 Rectangle 的大小,这样我就知道我在使用什么,并将 btyeArray 发布到这个方法中,它为我漂亮地写出了一个 PNG。

它有点蓝,真的很蓝,我不太清楚为什么,看看: http ://ec2-52-89-85-67.us-west-2.compute.amazonaws.com/imagedata/ppd/ 3898ae89-e4e0-4d03-97d7-dac4e3b618d5.png 它应该是黑色的: http://ec2-52-89-85-67.us-west-2.compute.amazonaws.com/imagedata/ppd/capture。 PNG

所以你们中的任何一个 .Net 人都知道发生了什么并可以建议我(我在某处听说我需要切换颜色的顺序,所以它会被切换而不是 rgb,但我不知道这将如何或为什么会成为问题)。

编辑这是解决方案,它很慢,没有意义,而且需要很长时间: http: //www.codeproject.com/Articles/2056/Image-Processing-for-Dummies-with-C-and-GDI-Part -3

或者,如果我们可以解决实际的 Flex 问题,那就太好了,尽管我已经放弃了 SO 对 Flex 支持有用的所有希望

谢谢大家的耐心。

于 2015-10-12T13:52:30.097 回答