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. 分布式缓存系统Memcached简介与实践

    缘起: 在数据驱动的web开发中,经常要重复从数据库中取出相同的数据,这种重复极大的增加了数据库负载.缓存是解决这个问题的好办法.但是ASP.NET中的虽然已经可以实现对页面局部进行缓存,但还是不够灵 ...

  2. 最简单的Web服务器

    //读取浏览器发过来的内容Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, Protoco ...

  3. valgrind检查C++内存泄漏

    valgrind --tool=memcheck --leak-check=full ./httptest Valgrind 使用 用法: valgrind [options] prog-and-ar ...

  4. [转]c++ vector 遍历方式

    挺有趣的,转来记录 随着C++11标准的出现,C++标准添加了许多有用的特性,C++代码的写法也有比较多的变化.   vector是经常要使用到的std组件,对于vector的遍历,本文罗列了若干种写 ...

  5. 前端模板artTemplate,handlerbars的使用心得

    写前端页面肯定离不开模板渲染,就近期项目中用的两个前端模板做一些使用总结,顺便复习一下,也方便后面温故. 1,artTemplate 优点: 1,一般web端用得较多,执行速度通常是 Mustache ...

  6. 使用 Laravel 框架:成为微信公众平台开发者

    转: http://ninghao.net/blog/1441 作者:王皓发布于:2014-05-30 13:16更新于:2014-05-31 12:05 我们可以使用Laravel 框架为微信公众平 ...

  7. Cash Cow【dfs较难题应用】【sdut2721】

    Cash Cow Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 题目链接:http://acm.sdut.edu.cn/sdut ...

  8. Powershell查看SSAS Cube占用磁盘空间

    以下是用powershell查看Cube占用磁盘空间大小的方式.可以编译成函数也可以直接把参数改成需要的服务器名称. Param($ServerName="SERVERNAME") ...

  9. [Java][Weblogic] weblogic.net.http.SOAPHttpsURLConnection incompatible with javax.net.ssl.HttpsURLConnection解决办法

    更新20141120: 我始终对修改生产上weblogic上的配置文件这一方法心存担忧(生产上的服务器不允许随便修改,可能会影响到其他应用),所以想使用代码的方式解决此问题,在对方法一失败原因进行了进 ...

  10. 配置ogg异构mysql-oracle 单向同步

    从mysql到oracle和oracle到mysql差不多.大致步骤如下: 环境是:192.168.0.165 (Mysql ) —> 192.168.0.164 ( Oracle )想将mys ...