我陷入了一個討厭的問題。通過WCF下載圖像爲字節數組失敗
有一個數據庫包含圖像作爲字節數組。我用下面的方法從字節數組獲取圖像回:
// byte[] ImageData contains the image loaded from the DB.
var stream = new MemoryStream(ImageData);
Image img = Image.FromStream(stream);
img.Save("D://Test/test.png", ImageFormat.Png);
當客戶端從我運行服務器和客戶端側的上面的代碼(具有不同的文件名)的服務器請求的圖像。
事情是:當我在服務器端,這很奇妙。文件保存,我可以在Windows照片查看器中打開並查看圖像。
但是,當我到達客戶端時,似乎這個字節數組已經發生了一些變化,Image引發了一個抱怨無效參數的異常。
此外,我試圖保存字節數組,因爲它是與.png擴展名,然後用照片查看器打開它。它也失敗了。
當通過WCF旅行時,我的數據可能會發生什麼,導致錯誤的字節數組?
編輯:在服務器和客戶端比較文件的結果。
數組看起來兩邊都是一樣的,除了在客戶端它們錯過了最後幾個字節。 儘管客戶端數組實際上有LESS數據,但文件大小說明客戶端圖像比BIGGER(正好是55字節)要多於服務器端。
服務器沒什麼特別。客戶端調用DownloadImg在BackgroundThread,而是直接對這樣的服務的參考:
var result = _service.DownloadImg(id);
服務接口:
[ServiceContract(Namespace="http://cannottellyou.com")]
public interface IImageService
{
[OperationContract]
DBImage DownloadImg(int id);
}
Image對象:
[DataContract(Namespace="http://cannottellyou.com")]
public class DBImage
{
[DataMember]
public virtual byte[] Data { get; set; }
}
功能加載圖像在服務器上:
public class ImgService: IImageService
{
public DBImage DownloadImg(int id)
{
var img = new DBImage();
//That is where we get the byte array, and where I tested the image with the above code, and it works. I am sorry I cannot tell you more about this part...
// img.Data holds the byte[] by now
return img;
}
}
結合:
<binding name="BinBinding" messageEncoding="Mtom"
closeTimeout="00:10:00"
openTimeout="00:01:00"
sendTimeout="00:05:00"
receiveTimeout="00:10:00"
maxReceivedMessageSize="67108864"
maxBufferPoolSize="65536">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="2097152"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<message />
</security>
</binding>
你來發表您的WCF接口/方法,綁定以及客戶端調用代碼,我們來看看 – 2012-04-03 13:09:27
嗯,你似乎很不願意告訴我們最重要的部分。那麼什麼是DBImage?如果你不想透露,你有沒有試圖返回一個簡單的ByteArray? – 2012-04-03 16:24:18
@MagnusJohansson:DBFile是2個字符串和一個字節數組:) – TDaver 2012-04-03 17:13:41