问题转换成字节数组翻番 [英] Problem to convert byte array to double

查看:123
本文介绍了问题转换成字节数组翻番的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用一个字节数组转换为双阵列问题 BitConverter.ToDouble()



只是我的计划会选择一个图像,然后将图像转换为字节数组。
,它可以将字节数组转换为双阵列。



这时候我的字节数组转换为双I将循环之前得到这个错误的问题光洁度。



(目标数组不够长,所有的项目复制收藏。检查数组索引和长度。)



错误的是阵列上的最后一个位置前的最后一次第七的位置array.Length-7的位置正好发生。



我需要帮助来解决这个问题在这里是我的代码:

 私人位图loadPic; 
字节[] imageArray;
双[] dImageArray;

私人无效btnLoad_Click(对象发件人,EventArgs五)
{

{
打开文件对话框打开=新的OpenFileDialog();
open.Filter =图像文件(* JPG; * .JPEG,* .gif和* .BMP)| * .JPG * .JPEG,* .gif和* .BMP;

如果(open.ShowDialog()== DialogResult.OK)
{
pictureBox1.Image =新位图(open.FileName);
loadPic =新位图(pictureBox1.Image);
}
}

{
抛出新ApplicationException的(无法加载图片);
}

pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}

私人无效btnConvert_Click(对象发件人,EventArgs五)
{
imageArray = imageToByteArray(loadPic);
INT指数= imageArray.Length;
dImageArray =新的双[指数]

的for(int i = 0; I<指数;我++)
{
dImageArray [I] = BitConverter.ToDouble(imageArray,I);
}
}

公共字节[] imageToByteArray(图像imageIn)
{
的MemoryStream毫秒​​=新的MemoryStream();
imageIn.Save(MS,ImageFormat.Gif);
返回ms.ToArray();
}


解决方案

  BitConverter.ToDouble(字节[],INT)

使用八个字节来构造64-位双,这也解释了你的问题(一旦你到了7日最后一个元素,不再有八个字节左)。我猜这是不会你想做的事,根据你如何设置你的循环是什么。



我想你想要的东西像

 的for(int i = 0; I<指数;我++)
{
dImageArray [ I] =(双)imageArray [I]
}



编辑 - 或使用LINQ,只是为了好玩:

 双击[] = dImageArray imageArray.Select(I =>(双)I).ToArray(); 

在另一方面...



如果 BitConverter 你想要什么肯定,那么你就需要这样的:

 双击[] = dImageArray新的双[imageArray.Length / 8]。 
的for(int i = 0; I< dImageArray.Length;我++)
{
dImageArray [I] = BitConverter.ToDouble(imageArray,我* 8);
}

再次根据你的代码,我认为第一种方法是你所需要的


I have a problem to convert an byte array to double array using BitConverter.ToDouble().

Simply my program will select an image then convert the image to byte array. Then it will convert the byte array to double array.

The problem that when I convert the byte array to the double I will get this error before the loop finish.

(Destination array is not long enough to copy all the items in the collection. Check array index and length.)

The error happen exactly at array.Length-7 position which is last seventh position before the last position on the array.

I need help to solve this problem and here is my code:

private Bitmap loadPic;
byte[] imageArray;
double[] dImageArray;

private void btnLoad_Click(object sender, EventArgs e)
{
    try
    {
        OpenFileDialog open = new OpenFileDialog();
        open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";

        if (open.ShowDialog() == DialogResult.OK)
        {
            pictureBox1.Image = new Bitmap(open.FileName);
            loadPic = new Bitmap(pictureBox1.Image);
        }
    }
    catch
    {
        throw new ApplicationException("Failed loading image");
    }

    pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}

private void btnConvert_Click(object sender, EventArgs e)
{
    imageArray =  imageToByteArray(loadPic);
    int index = imageArray.Length;
    dImageArray = new double[index];

    for (int i = 0; i < index; i++)
    {
        dImageArray[i] = BitConverter.ToDouble(imageArray,i);
    }
}   

public byte[] imageToByteArray(Image imageIn)
{
    MemoryStream ms = new MemoryStream();
    imageIn.Save(ms, ImageFormat.Gif);
    return ms.ToArray();
}

解决方案

BitConverter.ToDouble(byte[], int)

uses eight bytes to construct a 64-bit double, which explains your problem (once you get to the 7th to last element, there are no longer eight bytes left). I'm guessing this is not what you want to do, based on how you set up your loop.

I imagine you want something like:

for(int i = 0; i < index; i++)
{
    dImageArray[i] = (double)imageArray[i];
}

Edit - or using LINQ, just for fun:

double[] dImageArray = imageArray.Select(i => (double)i).ToArray();

On the other hand...

If BitConverter is definitely what you want, then you'll need something like:

double[] dImageArray = new double[imageArray.Length / 8];
for (int i = 0; i < dImageArray.Length; i++)
{
    dImageArray[i] = BitConverter.ToDouble(imageArray, i * 8);
}

Again, based on your code, I think the first solution is what you need.

这篇关于问题转换成字节数组翻番的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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