在打印图像到画布上时,通常需要在图像周围添加额外的边缘,以便将打印出的画布图像安装到木制框架上。本文将介绍一个工具,它能够为图像添加边缘和框架,以满足打印和装裱的需求。
该工具提供了创建纯色框架或镜像框架的功能。镜像框架更受欢迎,因为装裱后的边缘更加无缝且平滑。
工具界面由一个PictureBox和一个按钮组成:
以下是原始图像和转换后带有镜像边缘的图像示例:
代码实现相对标准,涉及到几个关键的位图转换操作。
加载的位图在整个过程中都保存在内存中,保持原始和完整。但是,为了能够在PictureBox中显示图片,如果图片大于PictureBox,则需要对其进行缩放:
private Bitmap resizePic(Bitmap original, float scale) {
if (scale == 1) return original;
Bitmap bmp = new Bitmap((int)(original.Width * scale), (int)(original.Height * scale));
Graphics g = Graphics.FromImage(bmp);
g.InterpolationMode = InterpolationMode.High;
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.DrawImage(original, new Rectangle(0, 0, (int)(original.Width * scale), (int)(original.Height * scale)), new Rectangle(0, 0, original.Width, original.Height), GraphicsUnit.Pixel);
return bmp;
}
缩放比例是图片与PictureBox的宽度和高度比例的最小值:
scale = Math.Min((float)pictureBox1.Width / (float)editPic.Width, (float)pictureBox1.Height / (float)editPic.Height);
private Bitmap addFrames(Bitmap original, int frameWidth) {
Bitmap bmp = addFrames(original, frameWidth, Color.Black);
Bitmap retBmp = new Bitmap(bmp.Width, bmp.Height);
Graphics g = Graphics.FromImage(retBmp);
g.InterpolationMode = InterpolationMode.High;
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.AntiAlias;
// draw original
g.DrawImage(original, new Rectangle(frameWidth, frameWidth, original.Width, original.Height), new Rectangle(0, 0, original.Width, original.Height), GraphicsUnit.Pixel);
// draw left and right frame
original.RotateFlip(RotateFlipType.RotateNoneFlipX);
g.DrawImage(original, new Rectangle(0, frameWidth, frameWidth, original.Height), new Rectangle(original.Width - frameWidth, 0, frameWidth, original.Height), GraphicsUnit.Pixel);
g.DrawImage(original, new Rectangle(original.Width + frameWidth, frameWidth, frameWidth, original.Height), new Rectangle(0, 0, frameWidth, original.Height), GraphicsUnit.Pixel);
original.RotateFlip(RotateFlipType.RotateNoneFlipX);
// draw upper and lower frame
original.RotateFlip(RotateFlipType.RotateNoneFlipY);
g.DrawImage(original, new Rectangle(frameWidth, 0, original.Width, frameWidth), new Rectangle(0, original.Height - frameWidth, original.Width, frameWidth), GraphicsUnit.Pixel);
g.DrawImage(original, new Rectangle(frameWidth, original.Height + frameWidth, original.Width, frameWidth), new Rectangle(0, 0, original.Width, frameWidth), GraphicsUnit.Pixel);
original.RotateFlip(RotateFlipType.RotateNoneFlipY);
// draw corners
original.RotateFlip(RotateFlipType.RotateNoneFlipXY);
g.DrawImage(original, new Rectangle(0, 0, frameWidth, frameWidth), new Rectangle(original.Width - frameWidth, original.Height - frameWidth, frameWidth, frameWidth), GraphicsUnit.Pixel);
g.DrawImage(original, new Rectangle(original.Width + frameWidth, 0, frameWidth, frameWidth), new Rectangle(0, original.Height - frameWidth, frameWidth, frameWidth), GraphicsUnit.Pixel);
g.DrawImage(original, new Rectangle(0, original.Height + frameWidth, frameWidth, frameWidth), new Rectangle(original.Width - frameWidth, 0, frameWidth, original.Height), GraphicsUnit.Pixel);
g.DrawImage(original, new Rectangle(original.Width + frameWidth, original.Height + frameWidth, frameWidth, frameWidth), new Rectangle(0, 0, frameWidth, frameWidth), GraphicsUnit.Pixel);
original.RotateFlip(RotateFlipType.RotateNoneFlipXY);
return retBmp;
}
private Bitmap addFrames(Bitmap original, int frameWidth, Color solidColor) {
int width = (int)(original.Width + 2 * frameWidth);
int height = (int)(original.Height + 2 * frameWidth);
Bitmap bmp = new Bitmap(width, height);
Graphics g = Graphics.FromImage(bmp);
g.InterpolationMode = InterpolationMode.High;
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.AntiAlias;
var brush = new SolidBrush(solidColor);
g.FillRectangle(brush, new RectangleF(0, 0, width, height));
g.DrawImage(original, frameWidth, frameWidth, original.Width, original.Height);
return bmp;
}