GDI+ 如何将图片绘制成圆形的图片
2016-03-11 11:48
1021 查看
大概意思就是不生成新的图片,而是将图片转换为圆形图片。
实现代码如下:
然后调用方法即可。
那么用WPF如何实现?实现就更简单了,不需要写后台代码,直接利用XAML即可。
————————————————————————————————————
群里的@陈应钦对如上的方法提出一个问题,就是如上的方法图形的下边缘和右侧边缘是一条直线,并不是平滑的曲线。细心观察确实如此。
原因是我画到了picturebox控件中,本身picturebox控件border就占长度,正好又画满控件,所以右边和下边都是直线了(空间不够了)。
所以解决方案有两个:可以直接绘制到不占空间的区域(比如窗体中指定的Rectangle),或者将绘制图形的长和宽缩小一点。
实现代码如下:
private Image CutEllipse(Image img, Rectangle rec, Size size) { Bitmap bitmap = new Bitmap(size.Width, size.Height); using (Graphics g = Graphics.FromImage(bitmap)) { using (TextureBrush br = new TextureBrush(img,System.Drawing.Drawing2D.WrapMode.Clamp, rec)) { br.ScaleTransform(bitmap.Width / (float)rec.Width, bitmap.Height / (float)rec.Height); g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; g.FillEllipse(br, new Rectangle(Point.Empty, size)); } } return bitmap; }
然后调用方法即可。
Image image = this.pictureBox1.Image; Image newImage = CutEllipse(image, new Rectangle(0, 0, 150, 150), new Size(150, 150)); this.pictureBox2.Image = newImage;
那么用WPF如何实现?实现就更简单了,不需要写后台代码,直接利用XAML即可。
<Grid> <Ellipse Height="150" Width="150"> <Ellipse.Fill> <ImageBrush ImageSource="Image/1_guwei4037.jpg" ></ImageBrush> </Ellipse.Fill> </Ellipse> </Grid>
————————————————————————————————————
群里的@陈应钦对如上的方法提出一个问题,就是如上的方法图形的下边缘和右侧边缘是一条直线,并不是平滑的曲线。细心观察确实如此。
原因是我画到了picturebox控件中,本身picturebox控件border就占长度,正好又画满控件,所以右边和下边都是直线了(空间不够了)。
所以解决方案有两个:可以直接绘制到不占空间的区域(比如窗体中指定的Rectangle),或者将绘制图形的长和宽缩小一点。
相关文章推荐
- C# GDI在控件上绘图的方法
- C#使用GDI绘制直线的方法
- C#使用GDI画圆的方法
- C#使用GDI绘制矩形的方法
- 使用C++绘制GDI位图的基本编写实例
- VC中使用GDI+的配置方法概述
- Visual C++程序设计中Windows GDI贴图闪烁的解决方法
- 如何基于纯GDI实现alpha通道的矢量和文字绘制
- GDI+学习及代码总结之------文本与字体
- vc 使用api绘制多边形
- vc gdi 位图的使用
- vc 将位图多余部分变为透明
- VC++中的CDC类
- GetWindowRect与GetClientRect
- 【Chapter 1】入门
- GDI+基础知识一
- GDI+基础知识三 钢笔(pen)和笔刷(brush) 1
- 【Chapter 2】基于Chp1的实例详解
- 【Chapter 3】基于Chp2的贪吃蛇的例子
- CreateCompatibleDC