我正在尝试使用C#将(图像的)字节数组保存到文件系统,并且无法使用我尝试过的任何一种方法工作,这是代码示例1:
string path = @"c:\ppd" + g.ToString() + ".jpg";
using (MemoryStream inputStream = new MemoryStream(image))
{
Image returnImage = Image.FromStream(inputStream);
returnImage.Save(path, ImageFormat.Jpeg);
}
使用此代码,将数据发送到服务时,我得到“参数无效”。因此,显然这是该数组的错误代码,因此我点击了google并提出了以下代码:
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);
}
}
}
这段代码将文件写入文件系统,但是当您尝试打开它时,它会很有帮助地说:“这不是有效的位图文件,或者当前不支持其格式。
我们使用以下方法从Flex获取byteArray:
bitmapData.getPixels(0, 0, bitmapData.width, bitmapData.height);
bitmapData来自Android相机(通过Distriqt Camera ANE)。该图像在应用中显示正常,但是我不知道下一步该怎么做,请帮忙。
编辑:Flex中获取byteArray的代码是:
<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具有3个参数,类型:字符串,id:int = 0,图像:ByteArray = null。
第二编辑:这是创建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));
}
第三编辑:我进行了一些测试,发现此代码应该工作,这是上面第一个块的变体,抛出“参数无效”
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);
所以现在我可以确定错误是出在byteArray上,但是我真的看不到我在做什么错。我有bitmapData并且它是有效的,并且看起来我正在收集所需的数据,写入的文件大小接近700Kb,但是出了点问题。如果有人能告诉我这个方程式在Flex方面出了什么问题,我将永远感激不已。我在显示列表上有BitmapImage,它已正确绑定到模型,并且我尝试了多种方法来指向源并绘制矩形。
好的,@ 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支持有用的所有希望,但也许我们可以解决实际的Flex问题。
谢谢大家的耐心配合。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句