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;
}

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

  1. HTML6注册表单输入日志标题

    一.找到元素. var d = document.getElementById("") var d = document.getElementsByName("" ...

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

    凡客首页使用Swiper和zepto,没有使用jquery , 静态首页+js交互,  资源加载使用 lazyLoad X-AspNet-Version: 4.0.30319 X-AspNetMvc- ...

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

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

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

    安装Oracle 11gR2,报错:[INS-06101] IP address of localhost could not be determined 出现这种错误是因为主机名和/etc/host ...

  5. MySQL的共享锁阻塞会话案例浅析输入日志标题

        这是问题是一个网友遇到的问题:一个UPDATE语句产生的共享锁阻塞了其他会话的案例,对于这个案例,我进一步分析.总结和衍化了相关问题.下面分析如有不对的地方,敬请指正.下面是初始化环境和数据的 ...

  6. VisualSVN设置提交时必须输入日志信息

    VisualSVN设置提交时必须输入日志信息 1.svn提交时强制输入提交信息 为了阻止SVN提交空日志信息和垃圾文件可以在SVN服务器端强制必须填写日志信息,这时需用到pre-commit钩子脚本. ...

  7. 正则表达式——WPF输入控件TextBox 限定输入特定字符

    概念: 正则表达式是对字符串操作的一种逻辑公式, 就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”, 这个“规则字符串”用来表达对字符串的一种过滤逻辑. 目的: 给定一个正 ...

  8. SVN提交强制输入日志信息

    在团队开发时,因一些团队成员提交代码时没有写提交说明的习惯,为了规范版本管理,增强大家的团队意识,上网找到了强制录入提交日志的方法.方法如下: 一.编写bat文件,命名为pre-commit.bat, ...

  9. WPF 实现带标题的TextBox

    这篇博客将分享在WPF中如何创建一个带Title的TextBox.首先请看一下最终的效果, 实现思路:使用TextBlock+TextBox来实现,TextBlock用来显示Title. 实现代码, ...

随机推荐

  1. Codeforces Round #370 (Div. 2)(简单逻辑,比较水)

    C. Memory and De-Evolution time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  2. iOS - property,strong,weak,retain,assign,copy,nomatic 的区别及使用

    1:ARC环境下,strong代替retain.weak代替assign,xcode 4.2(ios sdk4.3和以下版本)和之前的版本使用的是retain和assign,是不支持ARC的.xcod ...

  3. json_encode() 和 json_decode()

    php json_decode返回数据js的处理,json_decode后,返回到前台的数据如:encode_str => {"green":10,"size&qu ...

  4. Sample Apps by Android Team -- Amazed

    Sample Apps by Android Team 代码下载:http://pan.baidu.com/s/1eSNmdUE , 代码原地址:https://code.google.com/arc ...

  5. sql学习笔记--存储过程

    存储过程(stored procedure)有时也称sproc,它是真正的脚本,更准确地说,它是批处理(batch),但都不是很确切,它存储与数据库而不是单独的文件中. 存储过程中有输入参数,输出参数 ...

  6. Introduction to replication 翻译

    翻译自用,还有很多谬误之处,敬请甄别,转载请注明出处 Introduction to replication (replication介绍)   Replication is one of the m ...

  7. POJ 1061 同余方程

    两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是 它们出发之前忘记了一件很重要的事情,既没有问清楚对方的 ...

  8. 计算G711语音的打包长度和RTP里timestamp(时间戳)的增长量

    转自:http://blog.csdn.net/xujianglun/article/details/48342367 如何计算G711语音等的打包长度和RTP里timestamp的增长量 一般对于不 ...

  9. 智能车学习(十七)——舵机学习

    一.舵机的结构      舵机简单的说就是集成了直流电机.电机控制器和减速器等,并封装在一个便于安装的外壳里的伺服单元.能够利用简单的输入信号比较精确的转动给定角度的电机系统.舵机安装了一个电位器(或 ...

  10. sql修改约束语法练习

    --以系统管理员身份登录到SQL Server服务器,并使用T-SQL语句实现以下操作:--1. 将stu数据库中student表的sno定义为主键:alter table [student] add ...