久久久久久久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(刪除菜單項周圍的細邊框)
主站蜘蛛池模板: 91精品综合久久久久久五月天 | 欧美综合久久 | 日韩成人在线电影 | 成人日批视频 | 成人黄色在线 | 国产成人精品免费视频 | 四虎影院在线播放 | 亚洲天堂一区二区 | 密色视频 | 国产精品视频免费观看 | av免费在线播放 | 日本天堂一区 | 久久高清国产 | 免费成人在线网站 | 97日日碰人人模人人澡分享吧 | 亚洲一区三区在线观看 | 久久一久久 | 日韩成人久久 | 国产高清在线精品 | 亚洲视频不卡 | 亚洲v日韩v综合v精品v | 粉嫩粉嫩芽的虎白女18在线视频 | 黄a免费看 | 中文字幕在线播放第一页 | 亚洲精品视频在线播放 | 岛国av免费在线观看 | 91 视频网站 | 在线成人www免费观看视频 | 亚洲精品久久久久国产 | 免费观看一级特黄欧美大片 | 一区视频在线播放 | 亚洲视频在线免费观看 | 操久久久| 欧美乱大交xxxxx另类电影 | 亚洲在线一区 | 国产午夜精品视频 | 91精品一区二区三区久久久久久 | 国产精品电影在线观看 | 国产高清在线观看 | caoporn国产| 久久免费观看一级毛片 |