來源: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