我有一個問題,使用BitConverter.ToDouble()
將字節數組轉換爲雙數組。轉換字節數組爲雙倍的問題
只要我的程序將選擇一個圖像,然後將圖像轉換爲字節數組。 然後它會將字節數組轉換爲雙數組。
當我將字節數組轉換爲double時,在循環完成之前會出現此錯誤。
(目標數組不夠長,所有的項目複製收藏。檢查數組索引和長度。)
誤差在array.Length-7位究竟發生是之前的最後一個最後的第七位在陣列上的位置。
我需要幫助解決這個問題,這裏是我的代碼:
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完全不同的結果。 –
我剛剛澄清了我的帖子,指出基於他的循環(和他的數組初始化),我不認爲BitConverter是他想要的。 – Sapph
是的,我認爲你是對的。 +1 –