65.9K
CodeProject 正在变化。 阅读更多。
Home

图像画布边框编辑器

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.15/5 (3投票s)

2020年1月18日

CPOL

2分钟阅读

viewsIcon

6678

downloadIcon

212

用于为画布打印图像添加边框的小工具

引言

这是一个简单的工具,用于为位图图像添加边框(边缘),以便将其准备好打印在画布上。 这些边缘是必需的,因为打印的画布图片会安装在木质框架上,因此需要一些额外的边缘才能将其固定到框架上。

以下是一个说明该原理的示例

该工具使您可以创建纯色边框或镜像边框(后者更好,因为在装裱后边缘更无缝且更平滑)。

使用工具

该窗体包含一个 PictureBox 和一些在其下方的 button

  • 打开 - 打开一个 openFileDialog 以选择图片
  • 转换 - 添加边框;如果选中“纯色”checkbox,则边框将为纯色,否则将为镜像边缘。边框厚度 numericUpDown 确定边框的厚度。
  • 保存 - 保存结果图像;如果尚未进行“转换”,则此按钮不起作用。

这是原始图像和带有镜像边缘的转换图像的外观

代码

代码或多或少都是标准内容,我想指出几个位图转换。

原始图像与显示图像

加载的位图始终保存在内存中,原始且完整。 但是,为了能够在 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;
}

比例尺是图像的 widthheightpicturebox 的比率的最小值。

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, frameWidth), 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;
}

历史

  • 2020年1月18日:初始版本
© . All rights reserved.