首页 > 其他分享 >WPF image via web url or uri

WPF image via web url or uri

时间:2024-10-02 23:33:30浏览次数:7  
标签:web via url bmi System Windows imgBytes new using

The basic roadmap is to download web image at first ,

second  convert it into memeorystream,

third assign the memorystream to bitmapimage as StreamSource.

 

//xaml
<Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        WindowState="Maximized"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Image x:Name="img"/>
    </Grid>
</Window>


//xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static System.Net.Mime.MediaTypeNames;

namespace WpfApp2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        static string url = "https://wx4.sinaimg.cn/mw690/007ucPXmgy1hteb8tr7gjj311x1kwhdt.jpg";
        public MainWindow()
        {
            InitializeComponent();
            //LoadWebImageSync();
            LoadWebImageAsync();
        }

        private void LoadWebImageSync()
        {
            WebClient client = new WebClient();
            var imgBytes = client.DownloadData(url);
            if(imgBytes!=null && imgBytes.Any())
            {
                BitmapImage bmi = new BitmapImage();
                bmi.BeginInit();
                MemoryStream ms = new MemoryStream(imgBytes);
                ms.Seek(0, SeekOrigin.Begin);
                bmi.StreamSource = ms;
                bmi.EndInit();
                img.Source = bmi;
            }
        }

        private void LoadWebImageAsync()
        {
            WebClient client = new WebClient();
            client.DownloadDataAsync(new Uri(url));
            client.DownloadDataCompleted += Client_DownloadDataCompleted;
        }

        private void Client_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
        {
            var imgBytes = e.Result;
            if (imgBytes != null && imgBytes.Any())
            {
                MemoryStream stream = new MemoryStream(imgBytes);
                BitmapImage bmi = new BitmapImage();
                bmi.BeginInit();
                bmi.StreamSource = stream;
                bmi.EndInit();
                img.Source = bmi;
            }
        }
    }
}

 

 

 

 

Here I recommand the asynchronous method which will not block other thread.

 

 

标签:web,via,url,bmi,System,Windows,imgBytes,new,using
From: https://www.cnblogs.com/Fred1987/p/18445282

相关文章

  • WPF Click Window show XY coordinates in MVVM via InvokeCommandAction of behavior
    1.Install Microsoft.Xaml.Behaviors.Wpf  2.<behavior:Interaction.Triggers><behavior:EventTriggerEventName="MouseDown"><behavior:InvokeCommandActionCommand="{BindingClickCommand}"......
  • SpringBootWeb
    入门创建SpringBoot工程,勾选web开发相关依赖。packagecom.example;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;@RestController//这个注解是用来将一个类声明为一个控制器,并将其中的方......
  • Web Tomcat服务器
    但是Java的IDEA内嵌了Tomcat,不用下载。......
  • 【动态Web API学习(三)】动态方法
    1.应用程序模型ASP.NETCoreMVC根据控制器、操作、操作参数、路由和筛选器的结果,定义模型如下:ApplicationModel、控制器(ControllerModel)、操作(ActionModel)和参数(ParameterModel)。上一节中只是告诉系统封哪个是控制器,还要为控制器模型初始化值,比如路由、请求方式(post、get)、方......
  • pbootcms增加webp和mov等上传文件类型的方法
    在PBootCMS中,如果需要增加一些非常见的文件格式上传,可以通过修改配置文件来实现这一需求。以下是详细的步骤说明:操作步骤1.修改 config.php 文件打开 config.php 文件:文件位置:/config/config.php修改 upload 配置信息:在大约第30行附近找到 upload 配置......
  • 成功地在 PBootCMS 中增加 .webp 和 .mov 文件类型的上传支持
    config.php 文件修改示例<?phpreturnarray(//其他配置...//上传配置'upload'=>array('format'=>'jpg,jpeg,png,gif,xls,xlsx,doc,docx,ppt,pptx,rar,zip,pdf,txt,mp4,avi,flv,rmvb,mp3,otf,ttf,webp,mov',......
  • [CFI-CTF 2018]webLogon capture
    [CFI-CTF2018]webLogoncapture打开附件发现是流量分析题追踪TCP流发现密码解密得到flag,CFI{1ns3cur3_l0g0n}importbinasciistr='%20%43%46%49%7b%31%6e%73%33%63%75%72%33%5f%6c%30%67%30%6e%7d%20';print(binascii.a2b_hex(str.replace('%','')))......
  • php的urlencode和rawurlencode区别
    urlencode和rawurlencode都是用于对URL进行编码的函数,但它们在处理方式和应用场景上存在明显的区别。以下是关于这两个函数的详细比较:一、定义与标准urlencode:基于rawurlencode标准,但有略微的不同,它定义在rfc1866,这个rfc属于html标准的一部分,编码方式和application/x-www-for......
  • PySide 6 / PyQt 6 QWebEngineView 右键菜单汉化
    fromPySide6.QtWebEngineCoreimportQWebEnginePagefromPySide6.QtWebEngineWidgetsimportQWebEngineViewdefzh_CN(web:QWebEngineView):web.pageAction(QWebEnginePage.WebAction.NoWebAction).setText("")web.pageAction(QWebEnginePage.WebAc......
  • pom web 自动化测试框架分享
    这是初版的pomweb测试框架,目录如下同时部分代码也放在下面,详细代码可前往github 查看,欢迎大家给出宝贵意见。|--base|base_page.py(封装方法)||--config|allure_config.py(测试报告配置)||--data|code(验证码)|user.yaml(用户目录)||--logs|log(日......