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.


Figure 45


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:\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 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;
        }
时间: 2024-10-05 19:41:06

Image Transformation in WPF输入日志标题的相关文章

HTML6注册表单输入日志标题

一.找到元素. var d = document.getElementById("") var d = document.getElementsByName("") var d = document.getElementsByTagName("") var d = document.getElementsByClassName("")   二.操作元素. (一)操作内容 1.表单 value 赋值,取值   2.非表单 赋值,

MiS603开发板 第十七章 图像之HDMI接口测试输入日志标题

作者:MiS603开发团队 日期:20150911 公司:南京米联电子科技有限公司 论坛:www.osrc.cn 网址:www.milinker.com 网店:http://osrc.taobao.com EAT博客:http://blog.chinaaet.com/whilebreak 博客园:http://www.cnblogs.com/milinker/ MiS603开发板 第十七章 图像之HDMI接口测试 17.1 HDMI概述 HDMI是High Definition Multimed

扒一扒各大电商网站的m站都用的什么前端技术输入日志标题

凡客首页使用Swiper和zepto,没有使用jquery , 静态首页+js交互,  资源加载使用 lazyLoad X-AspNet-Version: 4.0.30319 X-AspNetMvc-Version: 3.0 X-Powered-By: ASP.NET Accept-Encoding: gzip, deflate, sdch 框架 zepto.js和jquery.js函数都差不多啊,zepto.js有什么优点呢     淘宝也使用了  zepto.js,还有 aplus.0.2.

搭载Azure的开发环境及相关资源的使用输入日志标题

搭载Azure的开发环境及相关资源的使用 从http://azure.microsoft.com/zh-cn/downloads/?rnd=1下载azure开发工具,Visual Studio 2013 update 4 ; 安装好之后,下载Azure SDK, 下载后,通过 Microsoft Web平台安装程序来获得并安装它们.接下来,你去可以微软下载官网,下载试用版的SQL-Server 2014 : 现在我们就可以导入Azure 订阅了: 首先,从https://manage.windo

C++类的公用继承输入日志标题

在定义一个派生类时将基类的继承方式指定为public的,称为公用继承,用公用继承方式建立的派生类称为公用派生类(public derived class ),其基类称为公用基类(public base class ). 采用公用继承方式时,基类的公用成员和保护成员在派生类中仍然保持其公用成员和保护成员的属性,而基类的私有成员在派生类中并没有成为派生类的私有成员,它仍然是基类的私有成员,只有基类的成员函数可以引用它,而不能被派生类的成员函数引用,因此就成为派生类中的不可访问的成员.公用基类的成员在

HTML4如何让一个DIV居中对齐?float输入日志标题

float:left,right clear:both 如何让一个DIV居中对齐? 第一步:设置外层的DIV的text-align:center; 第二步:设置里层的DIV的margin:auto 以上两个DIV都不要设置float.                  

Oracle 安装报错 [INS-06101] IP address of localhost could not be determined 解决方法输入日志标题

安装Oracle 11gR2,报错:[INS-06101] IP address of localhost could not be determined 出现这种错误是因为主机名和/etc/hosts 文件不一致,只需要把主机名和其IP 写入/etc/hosts 文件,就ok了. 查看主机名和和ip 地址关系: [[email protected]~]# cat /etc/sysconfig/network NETWORKING=yes NETWORKING_IPV6=no HOSTNAME=

C#中的String.Format方法输入日志标题

一.定义 String.Format是将指定的 String类型的数据中的每个格式项替换为相应对象的值的文本等效项. 如: (1) string p1 = "Jackie"; string p2 = "Aillo"; Response.Write(String.Format("Hello {0}, I'm {1}", p1, p2)); (2) Response.Write(String.Format("Hello {0}, I'm {

Component组件输入日志标题

Component    组件: Mesh:网格 Mesh Filter:网格过滤器 Text Mesh:文本网格 Mesh Renderer:网格渲染 Particles:粒子 Ellipsoid Particle Emitter:椭球粒子发射器 Mesh Particle Emitter:网格粒子发射器 Particle Animator:粒子动画 World Particle Collider:世界粒子对撞机 Particle Renderer:粒子渲染器 Trail Renderer:尾