Image transformation is a process of rotating and scaling images.
Rotating Images
There is ways to rotate an image. The Rotation property of BitmapImage and second option was use a transformbitmap image. The Transformbitmap class is use for both scaling and rotating images.
The Rotation property of BitmapImage was a type of Rotation enumeration that had four values Rotate0, Rotate90, Rotate180, and Rotate270. The following code snippet creates a BitmapImage element and set its Rotation attribute to Rotate270.
<image horizontalalignment= "Center" >
<Image.Source>
<bitmapimage urisource= "dock.jpg" rotation= "Rotate270"/>
</Image.Source>
</Image>
Figure: Shows the regular image and figure are the image rotates at degrees.
Figure 45
Figure 46
Alternatively, we can use the Transformbitmap and its Transform property to Transform an image. The Source attribute of Transformedbitmap is the image name. To rotate a image, we simply need to set the Transform property to RotateTransform and set Angle attribute to the Angle o F rotation as shown in below code.
<image >
<Image.Source>
<transformedbitmap source= "Dock.jpg" >
<TransformedBitmap.Transform>
<rotatetransform angle= "/>"
</TransformedBitmap.Transform>
</TransformedBitmap>
</Image.Source>
</Image>
The code listed in Listing-rotates an image at Run-time.
private void rotateimagedynamically ()
{
Create an Image
Image Imgcontrol = new Image ();
Create the Transformedbitmap
Transformedbitmap transformbmp = new Transformedbitmap ();
Create a BitmapImage
BitmapImage bmpimage = new BitmapImage ();
Bmpimage.begininit ();
Bmpimage.urisource = new Uri (@ "C:\Images\Dock.jpg", Urikind.relativeorabsolute);
Bmpimage.endinit ();
Properties must be set between BeginInit and EndInit
Transformbmp.begininit ();
Transformbmp.source = Bmpimage;
RotateTransform transform = new RotateTransform (90);
Transformbmp.transform = Transform;
Transformbmp.endinit ();
Set Image.source to Transformedbitmap
Imgcontrol.source = transformbmp;
LAYOUTROOT.CHILDREN.ADD (Imgcontrol);
}
Listing 42
Scaling Images
The ScaleTransform is used to scale an image. The ScaleX and ScaleY properties is used to resize, the image by the given factor. For example, value 0.5 reduces the image size by 50% and value 1.50 stretches image by 150%. The CenterX and CenterY properties are used to set the point of the the the the center of the scaling. By default, CenterX and CenterY values is 0 and 0 that represents the Top-left corner.
The following code snippet creates a BitmapImage element and set its ScaleTransform property and its attributes CenterX, C Entery, ScaleX, and ScaleY.
<ImageName= "ImageControl" ><Image.source><TransformedbitmapSource= "Dock.jpg" ><Transformedbitmap.transform><!--<rotatetransform angle= "/> " -<ScaleTransformCenterX= "+"CenterY= "+"ScaleX= "2"ScaleY= "2" /></Transformedbitmap.transform></Transformedbitmap></Image.source></Image>
Ways to get a picture of a resource in code:
Public StaticBitmapImage Getimageicon (System.Drawing.Bitmap Bitmap,Doubleangle) {BitmapImage BitmapImage=NewBitmapImage (); Try{System.IO.MemoryStream ms=NewSystem.IO.MemoryStream (); Bitmap. Save (MS, bitmap. Rawformat); Bitmapimage.begininit (); Bitmapimage.streamsource=MS; Bitmapimage.cacheoption=Bitmapcacheoption.onload; Bitmapimage.endinit (); Bitmapimage.freeze (); } Catch(Exception ex) {showerrormessage (ex); } returnBitmapImage; }
Image transformation in WPF input log header