开发者

How do I get an image from a file in a silverlight C# library into a writeable bitmap without using ImageLoaded event?

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;
      开发者_如何学Go  myIcon = new WriteableBitmap(bm);
    }

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


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜