zoukankan      html  css  js  c++  java
  • Image Transformation in WPF输入日志标题

    Image transformation is a process of rotating and scaling images.

    Rotating Images

    There are two ways to rotate an image. First option is to use the Rotation property of BitmapImage and second option is use a TransformBitmap image. The TransformBitmap class is use for both scaling and rotating images.

    The Rotation property of BitmapImage is a type of Rotation enumeration that has 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 44 shows the regular image and Figure 45 is the image rotates at 270 degrees.

    ImageTransformImg1.jpg

    Figure 45

    ImageTransformImg2.jpg

    Figure 46

    Alternatively, we can use TransformBitmap and its Transform property to transform an image. The Source attribute of TransformedBitmap is the image name. To rotate an image, we simply need to set the Transform property to RotateTransform and set Angle attribute to the angle of rotation as shown in below code.

    <Image >

    <Image.Source>

    <TransformedBitmap Source="Dock.jpg" >

    <TransformedBitmap.Transform>

    <RotateTransform Angle="90"/>

    </TransformedBitmap.Transform>

    </TransformedBitmap>

    </Image.Source>

    </Image>

    The code listed in Listing 42 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:ImagesDock.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 are 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 that is the center of the scaling. By default, CenterX and CenterY values are 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, CenterY, ScaleX, and ScaleY.

    <Image Name="ImageControl" >
    
    <Image.Source>
    
    <TransformedBitmap Source="Dock.jpg" >
    
    <TransformedBitmap.Transform>
    
    <!--<RotateTransform Angle="90"/>-->
    
    <ScaleTransform CenterX="25" CenterY="25" ScaleX="2" ScaleY="2" />
    
    </TransformedBitmap.Transform>
    
    </TransformedBitmap>
    
    </Image.Source>
    
    </Image>

     

    用代码获取资源图片的方法:

    public static BitmapImage GetImageIcon(System.Drawing.Bitmap bitmap, double angle)
            {
                BitmapImage bitmapImage = new BitmapImage();
    
                try
                {
    
                    System.IO.MemoryStream ms = new System.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);
                }
    
                return bitmapImage;
            }
  • 相关阅读:
    CAS 认证
    最近邻规则分类(k-Nearest Neighbor )机器学习算法python实现
    scikit-learn决策树的python实现以及作图
    module object has no attribute dumps的解决方法
    最新Flume1.7 自定义 MongodbSink 结合TAILDIR Sources的使用
    数据探索中的贡献度分析
    python logging模块按天滚动简单程序
    Flume性能测试报告(翻译Flume官方wiki报告)
    python apsheduler cron 参数解析
    python pyspark入门篇
  • 原文地址:https://www.cnblogs.com/xpvincent/p/4183846.html
Copyright © 2011-2022 走看看