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.
精彩评论