簡體   English   中英

如何將byte []轉換為BitmapFrame c#

[英]how to convert byte[] into BitmapFrame c#

我已經嘗試過但是有例外-由於對象的當前狀態,操作無效

private BitmapFrame backconvertor(byte[] incomingBuffer)
    {
        BitmapImage bmpImage = new BitmapImage();
        MemoryStream mystream = new MemoryStream(incomingBuffer);
        bmpImage.StreamSource = mystream;
        BitmapFrame bf = BitmapFrame.Create(bmpImage);
        return bf;
    }

當我嘗試時錯誤上升

return backconvertor(buff); 

在其他功能中(buff-准備好了!)

文檔指出,為了初始化圖像,您需要在BeginInitEndInit之間進行EndInit 那是:

bmpImage.BeginInit();
bmpImage.StreamSource = mystream;
bmpImage.EndInit();

或者,您可以將流傳遞給構造函數:

bmpImage = new BitmapImage(mystream);

有關示例和BeginInit更多討論,請參見http://msdn.microsoft.com/zh-cn/library/system.windows.media.imaging.bitmapimage.begininit.aspx

這是我在WPF轉換器中用於處理字節到BitmapFrame的內容,它可以完美地工作:

            var imgBytes = value as byte[];
            if (imgBytes == null)
                return null;
            using (var stream = new MemoryStream(imgBytes))
            {
                return BitmapFrame.Create(stream,
                    BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
            }

就像我之前在Task.Run中使用它一樣,它的線程也是安全的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM