久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

加載一個 BitmapSource 并在 WPF 中使用相同的名稱保

Load a BitmapSource and save using the same name in WPF -gt; IOException(加載一個 BitmapSource 并在 WPF 中使用相同的名稱保存 -IO異常)
本文介紹了加載一個 BitmapSource 并在 WPF 中使用相同的名稱保存 ->IO異常的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

當我嘗試保存我之前加載的 BitmapSource 時,會拋出一個 System.IO.IOException,指出另一個進程正在訪問該文件并且無法打開文件流.

When I try to save a BitmapSource that I loaded earlier, a System.IO.IOException is thrown stating another process is accessing that file and the filestream cannot be opened.

如果我只保存而不提前加載,一切正常.

If I only save whithout loading earlier, everything works fine.

加載代碼:

BitmapImage image = new BitmapImage();

image.BeginInit();
image.UriSource = uri;

if (decodePixelWidth > 0)
image.DecodePixelWidth = decodePixelWidth;

image.EndInit();

保存代碼:

using (FileStream fileStream = new FileStream(Directory + "\" + FileName + ".jpg", FileMode.Create))
{
    JpegBitmapEncoder encoder = new JpegBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create((BitmapImage)image));
    encoder.QualityLevel = 100;
    encoder.Save(fileStream);
}

似乎在加載圖像數據后,文件仍然被鎖定并且永遠不會被覆蓋,而打開它的應用程序仍在運行.任何想法如何解決這個問題?非常感謝您提供任何解決方案.

It seems like after loading the image data, the file is still locked an can never be overwritten while the application who opened it is still running. Any ideas how to solve this? Thanks alot for any solutions.

推薦答案

受到我對這個問題的評論的啟發,我通過將所有字節讀入內存流并將其用作 BitmapImage 的 Sreamsource 來解決該問題.

Inspired by the comments I got on this issue, I solved the problem by reading all bytes into a memorystream and using it as the BitmapImage's Sreamsource.

這個很完美:

if (File.Exists(filePath))
{
    MemoryStream memoryStream = new MemoryStream();

    byte[] fileBytes = File.ReadAllBytes(filePath);
    memoryStream.Write(fileBytes, 0, fileBytes.Length);
    memoryStream.Position = 0;

    image.BeginInit();
    image.StreamSource = memoryStream;

    if (decodePixelWidth > 0)
        image.DecodePixelWidth = decodePixelWidth;

    image.EndInit();
}

這篇關于加載一個 BitmapSource 并在 WPF 中使用相同的名稱保存 ->IO異常的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Right-click on a Listbox in a Silverlight 4 app(右鍵單擊 Silverlight 4 應用程序中的列表框)
WPF c# webbrowser scrolls over top menu(WPF c# webbrowser 在頂部菜單上滾動)
C# Console app - How do I make an interactive menu?(C# 控制臺應用程序 - 如何制作交互式菜單?)
How to avoid duplicate form creation in .NET Windows Forms?(如何避免在 .NET Windows Forms 中創建重復的表單?)
UI Automation Control Desktop Application and Click on Menu Strip(UI自動化控制桌面應用程序并單擊菜單條)
Removing thin border around the menuitems(刪除菜單項周圍的細邊框)
主站蜘蛛池模板: 国产精品欧美精品日韩精品 | 亚洲精品久久久久久久久久久 | 成人高清在线 | www四虎影视 | 天天操人人干 | 国产精品久久久久av | 国产成人一区 | 久久狠狠| 成人福利在线 | 成人精品国产免费网站 | 91精品国产91久久久久久最新 | www国产精品 | 在线免费国产视频 | 国产精品片aa在线观看 | 国产精品久久久久久久久久久新郎 | 91麻豆精品一区二区三区 | 午夜性色a√在线视频观看9 | 国产美女黄色片 | 一区二区三区精品在线视频 | 亚洲情综合五月天 | 日韩区| 日韩欧美在线免费观看视频 | 午夜视频免费网站 | 亚洲国产成人精品久久久国产成人一区 | 激情国产视频 | 欧美日韩亚洲成人 | 国产成人在线视频免费观看 | 日韩毛片播放 | 午夜在线观看视频 | 免费激情av | 久久国产精品视频 | 国产在线精品一区二区 | av黄在线观看 | 久久精品日产第一区二区三区 | 国内精品免费久久久久软件老师 | 亚洲iv一区二区三区 | 黄网站涩免费蜜桃网站 | 久久久久国产精品一区二区 | 日日夜夜精品视频 | 国产黄色大片在线观看 | 先锋av资源在线 |