GDI+ 如何将图片绘制成圆形的图片

大概意思就是不生成新的图片,而是将图片转换为圆形图片。

实现代码如下:

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>

你可能感兴趣的:(gdi)