如何在不使用 ImageLoaded 事件的情况下将 silverlight C# 库中的文件中的图像获取为可写位图?

发布于 2024-11-29 03:17:40 字数 987 浏览 1 评论 0原文

我需要在我的 silverlight 库中有一个图像并将其加载到位图中。我想把它当作资源来引用,但不知道如何去做。我在这个库中根本没有任何 xaml,但我读到的内容似乎表明我需要使用 xaml 来完成它。

以下是我在示例解决方案中使用 imageLoaded 事件执行此操作的方法。 (你知道 silverlight 多么喜欢异步的东西!)图像属性始终设置为资源/副本。

public partial class MainPage : UserControl
{
    WriteableBitmap myIcon = new WriteableBitmap(100, 100);

    public MainPage()
    {
        InitializeComponent();
        LoadImages();

    }

    public void LoadImages()
    {
        BitmapImage bmi = new BitmapImage();
        bmi.ImageOpened += ImagesLoaded;
        bmi.CreateOptions = BitmapCreateOptions.None;
        bmi.UriSource = new Uri(App.Current.Host.Source, "/ClientBin/HouseLogo.png");
    }


    public void ImagesLoaded(object sender, RoutedEventArgs e)
    {
        BitmapImage bm = (BitmapImage)sender;
        myIcon = new WriteableBitmap(bm);
    }

    private void btnPdf_Click(object sender, RoutedEventArgs e)
    {
        PDFdoc doc = new PDFdoc(32.0, 32.0, myIcon );
    }
}

I need to have an image in my silverlight library and load it into a bitmap. I want to just refer to it like a resource, but not sure how to go about it. I don't have any xaml at all in this library, but what I read seems to indicate I need to do it with xaml.

Here is how I did it in a sample solution using the imageLoaded event. (you know how silverlight just loves async stuff!) The image properties is set to resource/copy always.

public partial class MainPage : UserControl
{
    WriteableBitmap myIcon = new WriteableBitmap(100, 100);

    public MainPage()
    {
        InitializeComponent();
        LoadImages();

    }

    public void LoadImages()
    {
        BitmapImage bmi = new BitmapImage();
        bmi.ImageOpened += ImagesLoaded;
        bmi.CreateOptions = BitmapCreateOptions.None;
        bmi.UriSource = new Uri(App.Current.Host.Source, "/ClientBin/HouseLogo.png");
    }


    public void ImagesLoaded(object sender, RoutedEventArgs e)
    {
        BitmapImage bm = (BitmapImage)sender;
        myIcon = new WriteableBitmap(bm);
    }

    private void btnPdf_Click(object sender, RoutedEventArgs e)
    {
        PDFdoc doc = new PDFdoc(32.0, 32.0, myIcon );
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深巷少女 2024-12-06 03:17:40

首先,您说这是一个 silverlight 库,因此“内容”图像没有用,您需要在此库项目中的图像上指定“资源”构建操作。因此,访问图像资源所需的 Url 类似于“/YourLibraryNameDllName;component/Images/HouseLogo.png”。项目中有一个名为“Images”的文件夹,您可以在其中放置要从 dll 加载的 png。

完成此操作后,您可以使用这段代码将 png 加载到 WriteableBitmap 中,而无需使用异步模式。

 StreamResourceInfo sri = Application.GetResourceStream(new Uri("/YourLibraryNameDllName;component/Images/HouseLogo.png", UriKind.Relative));
 BitmapSource source = new BitmapImage();
 source.SetSource(sri.Stream);

 WriteableBitmap myIcon = new WriteableBitmap(source);

First of all you say this is a silverlight library hence "Content" images are not useful, you will need to specify the "Resource" build action on your images in this library project. Hence the Url you need to access the image resource is something like "/YourLibraryNameDllName;component/Images/HouseLogo.png". Where you have a folder in your project called "Images" where you are placing these pngs you want to load from your dll.

With that in place you can load the png into a WriteableBitmap without the async pattern using this chunk of code.

 StreamResourceInfo sri = Application.GetResourceStream(new Uri("/YourLibraryNameDllName;component/Images/HouseLogo.png", UriKind.Relative));
 BitmapSource source = new BitmapImage();
 source.SetSource(sri.Stream);

 WriteableBitmap myIcon = new WriteableBitmap(source);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文