Rounded Bitmap Corners

來源:互聯網
上載者:User

來源:http://www.imcoder.org/graphics/190937.htm

 

Q:

Hi there

I'm trying to create rounded corners on images programmatically. Is there an easy way to do this using Graphics.DrawImage function? I've used the following code to cut out a rectangle withing the image but how can i create a rectangle with rounded corners to use instead? any ideas?

 

Dim mybitmap as Bitmap = Image.fromfile( Server.Mappath("myimage.jpg") )

Dim cropbitmap as Bitmap = new Bitmap(500, 360)

Dim mygraphic as Graphics = Graphics.FromImage(cropbitmap)

mygraphic.DrawImage(mybitmap, new Rectangle(5,5,490,350),new Rectangle(0,0,500,360),GraphicsUnit.Pixel)

 

response.contenttype="image/jpeg"

cropbitmap.save(response.outputstream, imageformat.jpeg)

mygraphic.dispose()

cropbitmap.dispose()

mybitmap.dispose()

 

 

 

 

 

A:

found the answer

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Drawing" %>
<script runat="server">

    void Page_Init(Object sender,EventArgs e) {

        string path = Server.MapPath("~/Images/flower.jpg");
        int roundedDia = 50;

       
            using(Image imgin = Image.FromFile(path)){
            Bitmap bitmap = new Bitmap(imgin.Width, imgin.Height);
            Graphics g = Graphics.FromImage(bitmap);
            g.Clear(Color.White);
            g.SmoothingMode =
(System.Drawing.Drawing2D.SmoothingMode.AntiAlias);
            Brush brush = new System.Drawing.TextureBrush(imgin);
            FillRoundedRectangle(g,
new Rectangle(0, 0, imgin.Width, imgin.Height),
roundedDia, brush);

// done with drawing dispose graphics object.
            g.Dispose();
            // Stream Image to client.
            Response.Clear();
            Response.ContentType = "image/pjpeg";
            bitmap.Save(Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg);
            Response.End();
// dispose bitmap object.
            bitmap.Dispose();
           
        }
    }

    public static void FillRoundedRectangle(Graphics g,
Rectangle r,int d,Brush b){

        System.Drawing.Drawing2D.GraphicsPath gp
= new System.Drawing.Drawing2D.GraphicsPath();

        gp.AddArc(r.X, r.Y, d, d, 180, 90);
        gp.AddArc(r.X + r.Width - d, r.Y, d, d, 270, 90);
        gp.AddArc(r.X + r.Width - d, r.Y + r.Height - d, d, d, 0, 90);
        gp.AddArc(r.X, r.Y + r.Height - d, d, d, 90, 90);

        g.FillPath(b, gp);
    }
</script>

courtesy of http://www.jigar.net/howdoi/viewhtmlcontent98.aspx

A:

Hi

Does anybody know how to do this in VB? Ideally I'd like to run this on file upload and actually save the rounded image as a new file, rather than just generating it on-the-fly. But if anyone can translate this to VB, that'd be marvellous (code converters just don't work).

Thanks

A:

Not to worry, I worked it out:

Protected

Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

 

Dim path As String = Server.MapPath("flower.jpg")

 

Dim roundedDia As Integer = 50

 

 

Using imgin As System.Drawing.Image = System.Drawing.Image.FromFile(path)

 

Dim bitmap As Drawing.Bitmap = New Drawing.Bitmap(imgin.Width, imgin.Height)

 

Dim g As Drawing.Graphics = Drawing.Graphics.FromImage(bitmap)

g.Clear(System.Drawing.Color.White)

g.SmoothingMode = (System.Drawing.Drawing2D.SmoothingMode.AntiAlias)

 

Dim brush As Drawing.Brush = New System.Drawing.TextureBrush(imgin)

FillRoundedRectangle(g,

New Drawing.Rectangle(0, 0, imgin.Width, imgin.Height), roundedDia, brush)

 

' done with drawing dispose graphics object.

g.Dispose()

 

' Stream Image to client.

Response.Clear()

Response.ContentType =

"image/pjpeg"

bitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)

bitmap.Save(Server.MapPath(

"flower3.jpg"))

Response.End()

 

' dispose bitmap object.

bitmap.Dispose()

 

End Using

 

End Sub

 

Public Shared Sub FillRoundedRectangle(ByVal g As Drawing.Graphics, ByVal r As Drawing.Rectangle, ByVal d As Integer, ByVal b As Drawing.Brush)

 

Dim gp As System.Drawing.Drawing2D.GraphicsPath = New System.Drawing.Drawing2D.GraphicsPath()

gp.AddArc(r.X, r.Y, d, d, 180, 90)

gp.AddArc(r.X + r.Width - d, r.Y, d, d, 270, 90)

gp.AddArc(r.X + r.Width - d, r.Y + r.Height - d, d, d, 0, 90)

gp.AddArc(r.X, r.Y + r.Height - d, d, d, 90, 90)

g.FillPath(b, gp)

 

End Sub

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.