実際は画像の角を丸めたように見せるための方法です。
kadomarizrに触発されてVisual C#で実装してみました。本家のようにキレイにならないのはご愛敬ということで。最初角丸四角形のクリッピング領域を作る方法がわかりませんでしたが、Microsoftのサイトにサンプルがありましたのでなんとか実現できました。http://www.microsoft.com/japan/msdn/vbasic/migration/tips/graphic.aspx
使えるようにするにはもうちょっと作り込まないとならないのですが、とりあえずソースを掲載
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace kadomarizr
{
/// <summary>
/// 画像を生成します
/// </summary>
public sealed class Kadomarizer
{
public Kadomarizer()
{
}
/// <summary>
/// カドマライズします
/// </summary>
/// <param name="filename">ファイル名</param>
/// <returns></returns>
public bool kadomarize(string filename)
{
using(Bitmap srcbmp = new Bitmap(filename))
{
int width = srcbmp.Width;
int height = srcbmp.Height;
using(Bitmap destbmp = new Bitmap(width, height, PixelFormat.Format24bppRgb))
{
using(Graphics g = Graphics.FromImage(destbmp))
{
g.FillRectangle(Brushes.White, 0, 0, width, height);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
GraphicsPath path = this.CreateRoundedRectangle(width, height);
g.SetClip(path, CombineMode.Replace);
g.DrawImage(srcbmp, 0,0, width, height);
g.Dispose();
}
string savename = string.Format(@"{0}\{1}k.bmp",
System.IO.Path.GetDirectoryName(filename),
System.IO.Path.GetFileNameWithoutExtension(filename));
destbmp.Save(savename, ImageFormat.Bmp);
}
}
return true;
}
/// <summary>
/// 角丸領域のパスを生成します
/// </summary>
/// <param name="width">画像の幅</param>
/// <param name="height">画像の高さ</param>
/// <returns>GraphicsPath</returns>
/// <remarks>
/// </remarks>
private GraphicsPath CreateRoundedRectangle(int width, int height)
{
Size offset = new Size(width / 5, height / 5);
GraphicsPath path = new GraphicsPath(FillMode.Winding);
path.AddArc(width - offset.Width, 0, offset.Width, offset.Height, 270, 90);
path.AddArc(width - offset.Width, height - offset.Height, offset.Width, offset.Height, 0, 90);
path.AddArc(0, height - offset.Height, offset.Width, offset.Height, 90, 90);
path.AddArc(0, 0, offset.Width, offset.Height, 180, 90);
path.AddArc(width - offset.Width, 0, offset.Width, offset.Height, 270, 90);
return path;
}
}
}
最近のコメント