Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么WPF BitmapImage对象不从ASP.Net Web表单中的Uri源下载图像?_C#_Asp.net_Wpf - Fatal编程技术网

C# 为什么WPF BitmapImage对象不从ASP.Net Web表单中的Uri源下载图像?

C# 为什么WPF BitmapImage对象不从ASP.Net Web表单中的Uri源下载图像?,c#,asp.net,wpf,C#,Asp.net,Wpf,我试图在ASP.Net中实现以下目标: 创建WPF画布控件 启动WPF图像控件和BitmapImage对象 将BitmapImage源设置为图像的Uri 将图像添加到画布 下载图像时,将画布渲染为新位图 我的代码在WPF本身中正常工作,但是在ASP.Net页面中运行时,不会下载图像 对于其他WPF UI元素,它完全可以正常工作。对于图像,使用BitmapImage.StreamSource属性设置源可以正常工作。当我使用BitmapImage.UriSource属性时,不会引发BitmapIma

我试图在ASP.Net中实现以下目标:

  • 创建WPF画布控件
  • 启动WPF图像控件和BitmapImage对象
  • 将BitmapImage源设置为图像的Uri
  • 将图像添加到画布
  • 下载图像时,将画布渲染为新位图
  • 我的代码在WPF本身中正常工作,但是在ASP.Net页面中运行时,不会下载图像

    对于其他WPF UI元素,它完全可以正常工作。对于图像,使用BitmapImage.StreamSource属性设置源可以正常工作。当我使用BitmapImage.UriSource属性时,不会引发BitmapImage.DownloadCompleted事件,这表明映像从一开始就不会开始下载

    需要注意的是,只要我使用流源而不是uri源,它对大多数控件(椭圆、矩形、墨迹呈现器以及图像控件)都可以正常工作

    那么,我错过了什么?为什么BitmapImage类在web中的行为不同 申请

    我知道会有人问我,所以这样做的目的是我编写了一个Silverlight客户端来创建存储在web服务器上的图形内容。我希望web服务器将内容呈现为位图文件

    提前谢谢你的建议

    以下是我的ASP.Net页面代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Windows.Controls;
    using System.Windows.Media.Imaging;
    using System.Threading;
    using System.Windows;
    using System.Net;
    using System.IO;
    using System.Windows.Media;
    
    public partial class _Default : System.Web.UI.Page 
    {
        private static Canvas c;
        protected void Page_Load(object sender, EventArgs e)
        {
            Thread t = new Thread(new ThreadStart(
                delegate { DownloadAndSave(); }));
            t.SetApartmentState(ApartmentState.STA);
            t.Start();
            t.Join();
        }
    
        [STAThread]
        void DownloadAndSave()
        {
            c = new Canvas();
            BitmapImage bitmap = new BitmapImage();
            System.Windows.Controls.Image image = new System.Windows.Controls.Image();
    
            bitmap.DownloadCompleted += new EventHandler(bitmap_DownloadCompleted);
            bitmap.BeginInit();
            bitmap.UriSource = new Uri("http://andrew.myhre.tanash.net/customassets/andrewmyhre/image/face.jpg");
            bitmap.EndInit();
    
            image.Source = bitmap;
    
            c.Children.Add(image);
    
            c.UpdateLayout();
            c.Measure(new Size(400, 300));
            c.Arrange(new Rect(new Size(400, 300)));
        }
    
        void bitmap_DownloadCompleted(object sender, EventArgs e)
        {
            // this never fires!!
            SaveImage(c);
        }
    
        void SaveImage(UIElement element)
        {
            RenderTargetBitmap bmp = new RenderTargetBitmap(400, 300, 96, 96, PixelFormats.Pbgra32);
            bmp.Render(element);
            BitmapEncoder encoder = new JpegBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(bmp));
            using (Stream stm = File.Create(Server.MapPath("~/file.jpg")))
                encoder.Save(stm);
        }
    }
    

    您是否验证了将STAThread属性添加到DownloadAndSave方法实际上会使其在STA线程中运行

    根据STAThreadAttribute()的文档,它对主条目方法(例如WinForms应用程序中的main)所使用的其他方法没有任何影响


    我想您应该朝这个方向看-我在需要STA的WPF映像函数方面也遇到过类似的问题。

    我认为DownloadCompleted事件从未引发,因为当您使用允许后台线程的URI源属性(DownloadAndSave方法)时,BitmapImage会异步加载在加载图像之前完成


    最近,在几乎相同的情况下,我的工作方式是同步下载位图资源,并将其设置为BitmapImage.StreamSource属性。请注意,使用该技术时,只有在渲染RenderTargetBitmap之后才能关闭/处置流。

    没错,STAThread属性对该方法没有影响。直到现在我才知道为什么。。