我的Windows Phone應用程序保存由某些文本/數字信息(如圖像捕獲日期等)和一對JPEG圖像組成的對象。C#:保存並加載兩個JPEG圖像到一個文件或從一個文件加載
from byte 0 to 3 -----> INT NUMBER
from byte 4 to 11 ----> TWO INTs DESCRIBING THE SIZE OF THE NEXT IMAGE
from byte 12 to X ----> FIRST JPEG IMAGE
from byte X+1 to X+7 -> TWO INTs DESCRIBING THE SIZE OF THE NEXT IMAGE
from byte X+8 to Y ---> SECOND JPEG IMAGE
我已經實現了節省方法是這樣的::
public bool SaveDataToMemory(string filename)
{
try
{
IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication();
if (!appStorage.DirectoryExists(App.USER_FOLDER))
{
appStorage.CreateDirectory(App.USER_FOLDER);
}
if (appStorage.FileExists(filename))
{
appStorage.DeleteFile(filename);
}
IsolatedStorageFileStream fs = null;
using (fs = appStorage.CreateFile(filename))
{
int thumbW = (int)this.thumbnail.PixelWidth;
int thumbH = (int)this.thumbnail.PixelHeight;
int thumbLength = thumbW * thumbH;
int imageW = (int)this.imageSize.Width;
int imageH = (int)this.imageSize.Height;
int imageLength = imageW * imageH;
byte[] byteToWrite;
int bufferCount;
byteToWrite = BitConverter.GetBytes(DATA_VERSION); // THE FIRST INT NUMBER
fs.Write(byteToWrite, 0, byteToWrite.Length);
byteToWrite = BitConverter.GetBytes(thumbW); // THE FIRST COUPLE OF INTs
fs.Write(byteToWrite, 0, byteToWrite.Length);
byteToWrite = BitConverter.GetBytes(thumbH);
fs.Write(byteToWrite, 0, byteToWrite.Length);
this.thumbnail.SaveJpeg(fs, thumbW, thumbH, 0, 75); // THE FIRST IMAGE (WriteableBitmap)
byteToWrite = BitConverter.GetBytes(imageW); // THE SECOND COUPLE OF INTs
fs.Write(byteToWrite, 0, byteToWrite.Length);
byteToWrite = BitConverter.GetBytes(imageH);
fs.Write(byteToWrite, 0, byteToWrite.Length);
this.image.SaveJpeg(fs, imageW, imageH, 0, 92); // THE SECOND IMAGE
}
return true;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex);
return false;
}
}
的方法應該工作,至少有 例如和簡單,單個文件可以由以下數據組成的順序也不例外。 現在的問題是,當我嘗試將它們加載回來。 我按照之前描述的數據順序訪問數據流,我可以用WriteableBitmap.LoadJpeg
檢索並查看第一個圖像(縮略圖),但是當我繼續檢索下一個整數時,它們顯示爲0,第二個圖像無法顯示加載例外。
public bool LoadDataFromMemory(string filename)
{
try
{
IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream fs = null;
if (!appStorage.FileExists(filename)) return false;
using (fs = appStorage.OpenFile(filename, FileMode.Open))
{
if (fs == null) return false;
byte[] dataVersionBuf = new byte[sizeof(Int32)]; // FIRST INTEGER
fs.Read(dataVersionBuf, 0, dataVersionBuf.Length);
int dataVersion = BitConverter.ToInt32(dataVersionBuf, 0);
System.Diagnostics.Debug.WriteLine("Data version:" + dataVersion);
byte[] thumbSizeBuf = new byte[8];
fs.Read(thumbSizeBuf, 0, thumbSizeBuf.Length); // SIZE OF THE NEXT IMAGE
int thumbW = BitConverter.ToInt32(thumbSizeBuf, 0);
int thumbH = BitConverter.ToInt32(thumbSizeBuf, 4);
this.thumbnail = new WriteableBitmap(thumbW, thumbH);
this.thumbnail.LoadJpeg(fs); // FIRST IMAGE
// (this prints the correct information)
System.Diagnostics.Debug.WriteLine("Loaded thumbnail " + this.thumbnail.PixelWidth + "x" + this.thumbnail.PixelHeight);
byte[] leftSizeBuf = new byte[sizeof(Int32) * 2];
fs.Read(leftSizeBuf, 0, leftSizeBuf.Length); // <<--- PROBLEMS READING HERE!
int imageW = BitConverter.ToInt32(leftSizeBuf, 0);
int imageH = BitConverter.ToInt32(leftSizeBuf, 4);
// imageW and imageH are equal to 0!!!!
System.Diagnostics.Debug.WriteLine("Loading left " + imageW + "x" + imageH);
WriteableBitmap wb = new WriteableBitmap(imageW, imageH);
wb.LoadJpeg(fs); // <--- EXCEPTION HERE!
return true;
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex);
return false;
}
}
拋出的異常是System.ArgumentException: incorrect parameter
。 我是否實施了錯誤的方法將兩個圖像存儲在同一個流/文件中?
好東西,很高興我能幫上忙。 – Xenolightning