开发者

What assembly reference should I add in order to be able to create an instance of the Image-class (C#, WPF)?

开发者 https://www.devze.com 2023-03-16 15:50 出处:网络
I have created a Class Library containing several classes. What assembly reference should I add in order 开发者_开发技巧to be able to create an instance of the Image-class?

I have created a Class Library containing several classes. What assembly reference should I add in order 开发者_开发技巧to be able to create an instance of the Image-class? It is a WPF-application created in Visual Studio 2010 using C#.

My code follows if anyone is interested.

Thanks in advance! Anders Branderud

    /// <summary>
    /// Create an image with certain image address and return it.
    /// </summary>
    /// <param name="imageAddress">Name of image file without jpg ending.</param>
    /// <returns></returns>
    ///<remarks>Adapted code from Microsoft-website</remarks>
    public static BitmapImage CreateAndDisplayImage(string imageAddress)
    {
        // Create Image Element
        Image myImage = new Image();
        myImage.Width = 200;

        // Create source
        BitmapImage myBitmapImage = new BitmapImage();



        myBitmapImage.BeginInit();
        //Image address
        Uri relativeUri = new Uri(IMAGE_DIRECTORY + imageAddress + ".jpg", UriKind.Relative);
        myBitmapImage.UriSource = relativeUri;



        // IMAGE_DIRECTORY + "/"
        // To save significant application memory, set the DecodePixelWidth or  
        // DecodePixelHeight of the BitmapImage value of the image source to the desired 
        // height or width of the rendered image. If you don't do this, the application will 
        // cache the image as though it were rendered as its normal size rather then just 
        // the size that is displayed.
        // Note: In order to preserve aspect ratio, set DecodePixelWidth
        // or DecodePixelHeight but not both.
        myBitmapImage.DecodePixelWidth = 200;
        myBitmapImage.EndInit();
        //set image source
        return myBitmapImage;

}


Image (as in System.Drawing.Image) is abstract. Use Bitmap (System.Drawing.Bitmap) which derives from it instead. Unless, for whatever the reason, you're doing your own implementation of Bitmap.

0

精彩评论

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