Introduction
This is a simple tool that adds borders (edges) to a bitmap image to prepare it for printing on canvas. The edges are needed because the printed canvas picture is mounted onto wooden frames, so you need some extra edges to be able to attach it to the frame.
Here is an example that illustrates the principle:
The tool gives you an opportunity to create solid color frames or mirror frames (which is better because the edges are more seamless and smoother after framing).
Using the Tool
The form consists of a PictureBox
and some button
s underneath:
- Open - Opens an
openFileDialog
to choose the picture - Transform - Adds the frames; if Solid color
checkbox
is selected, then the frames shall be solid color, otherwise they shall be mirror edges. Frame thickness numericUpDown
determines the thickness of the frame. - Save - Saves the result image; this button doesn't work if the
Transform
has not been done yet.
This is how an original and transformed image with mirror edges would look like:
The Code
The code is more or less quite standard stuff, there are several bitmap transformations that I would like to point out.
Original Image vs. Shown Image
The bitmap that is loaded stays saved in memory the whole time, original and intact. However, to be able to show the picture in the picturebox
, it needs to be resized in case it is larger than the 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;
}
The scale is calculated as minimum of both ratios of width
s and height
s of the image against the picturebox
:
scale = Math.Min((float)pictureBox1.Width / (float)editPic.Width,
(float)pictureBox1.Height / (float)editPic.Height);
Adding Frames
The method for adding the frames has two overloads - one for solid color and one for mirror edges.
The one that creates the mirror edges first adds a default (black) edge to the bitmap, and then copies certain parts of the original image to fill the edges, flipping the original image appropriately each time.
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;
g.DrawImage(original, new Rectangle(frameWidth, frameWidth,
original.Width, original.Height),
new Rectangle(0, 0, original.Width, original.Height), GraphicsUnit.Pixel);
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);
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);
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;
}
History
- 18th January, 2020: Initial version