image图片解除占用 wpf_WPF Image图片后台绑定图片,不对图片照成占用

通过使用`using`语句和适当处理,本文介绍了一种在WPF中如何绑定图片并防止图片文件被占用的方法。在BinDingImag函数中,通过FileStream打开图片,然后转换为WPF的ImageSource,最后确保资源被正确释放。
摘要由CSDN通过智能技术生成

public void BinDingImag(string ImageName, Image image)

{

using (FileStream FS = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "Images//ManTitle//" + ImageName + ".png", FileMode.Open))

{

System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(FS);

image.Source = ConverImages.GetImageBrushFrom_PResource(bmp).ImageSource;

image.Stretch = Stretch.Fill;

bmp.Dispose();

FS.Dispose();

FS.Close();

}

}

using System;

using System.Collections.Generic;

using System.Drawing;

using System.IO;

using System.Linq;

using System.Text;

using System.Windows.Media;

ConverImages----类

public class ConverImages

{

///

/// 图片转换

///

///

///

public static ImageBru

WPF (Windows Presentation Foundation) 中,如果你想实现在用户界面上动态切换背景图片绑定到Bing每日壁纸服务,你可以采用以下几个步骤: 1. 首先,在XAML中设置一个`Image`控件作为背景,并将其`Source`属性设置为一个绑定路径,例如: ```xml <Image x:Name="BackgroundImage" Stretch="UniformToFill" HorizontalAlignment="Center" VerticalAlignment="Center"> <Image.Source> <BitmapImage UriSource="{x:Static local:BingWallpaperSourceUri}"/> </Image.Source> </Image> ``` 这里,`local:BingWallpaperSourceUri`是一个自定义的`StaticResource`,它会在运行时从Bing获取最新的壁纸URL。 2. 创建一个本地类来获取Bing壁纸的URL,比如`BingWallpaperSource.cs`: ```csharp public static class BingWallpaperSource { public static readonly DependencyProperty UriSourceProperty = DependencyProperty.Register( "UriSource", typeof(Uri), typeof(BingWallpaperSource), new PropertyMetadata(null, OnUriSourceChanged)); private static void OnUriSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ((Image)d).Source = new BitmapImage((Uri)e.NewValue); } // 获取Bing壁纸的API URL,这需要替换为你实际使用的API地址 public static Uri GetBingWallpaperUri() { return new Uri("https://www.bing.com/HPImageArchive.aspx?format=jpg&idx=0&n=1&mkt=en-US"); } public static Uri UriSource { get { return (Uri)GetValue(UriSourceProperty); } set { SetValue(UriSourceProperty, value); } } } ``` 3. 然后在你的视图模型或窗口的构造函数、启动事件或其他适当的地方,定期更新`BingWallpaperSource.UriSource`,如每小时一次: ```csharp private async void UpdateWallpaperTimer_Tick(object sender, EventArgs e) { UriSource = BingWallpaperSource.GetBingWallpaperUri(); await Task.Delay(TimeSpan.FromHours(1)); } ``` 4. 设置定时任务(如使用`DispatcherTimer`),以便每隔一段时间调用`UpdateWallpaperTimer_Tick`方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值