GDI + How to draw a picture into a circular picture

Source: Internet
Author: User

The idea is not to generate a new picture, but to convert the picture to a circular picture.

The implementation code is as follows:

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

Then the method can be called.

Image image = This.pictureBox1.Image;
Image newimage = Cutellipse (image, new Rectangle (0, 0,), new Size ();
This.pictureBox2.Image = NewImage;

So how do you implement WPF. Implementation is simpler, you don't need to write back code, you can use XAML directly.

<Grid>
        <ellipse height= "width=" >
            <Ellipse.Fill>
                <imagebrush ImageSource = "Image/1_guwei4037.jpg" ></ImageBrush>
            </Ellipse.Fill>
        </Ellipse>
</grid >

————————————————————————————————————

@ Chen Chuqin in a group the problem with the method above is that the bottom and right edges of the method graph above are a straight line, not a smooth curve. Careful observation is indeed so.

The reason is that I draw the PictureBox control, itself PictureBox control border the length, just the full control, so the right and bottom are straight line (space is not enough).

So there are two solutions: you can draw directly into areas that do not occupy space (such as the rectangle specified in a form), or you will narrow the length and width of the drawing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.