📄 imagetransform.cs
字号:
else
{
// 垂直翻转
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
dst = (byte*)dstScan0 + ((height - y - 1) * stride) + (x * BPP);
dst[0] = src[0]; // B
dst[1] = src[1]; // G
dst[2] = src[2]; // R
dst[3] = src[3]; // A
src += BPP;
} // x
src += offset;
} // y
} // isHorz
}
b.UnlockBits(srcData);
dstImage.UnlockBits(dstData);
return dstImage;
} // end of Flip
/// <summary>
/// 对图像进行转置变换
/// </summary>
/// <param name="b">原始图像</param>
/// <returns></returns>
public Bitmap Transpose(Bitmap b)
{
int width = b.Width;
int height = b.Height;
Bitmap dstImage = new Bitmap(height, width);
BitmapData srcData = b.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
BitmapData dstData = dstImage.LockBits(new Rectangle(0, 0, height, width),
ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
int srcStride = srcData.Stride;
int dstStride = dstData.Stride;
System.IntPtr srcScan0 = srcData.Scan0;
System.IntPtr dstScan0 = dstData.Scan0;
int offset = srcData.Stride - width * BPP;
unsafe
{
byte* src = (byte*)srcScan0;
byte* dst = (byte*)dstScan0;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
dst = (byte*)dstScan0 + (x * dstStride) + (y * BPP);
dst[0] = src[0];
dst[1] = src[1];
dst[2] = src[2];
dst[3] = src[3];
src += BPP;
} // x
src += offset;
} // y
}
dstImage.UnlockBits(dstData);
b.UnlockBits(srcData);
b.Dispose();
return dstImage;
} // end of Transpose
/// <summary>
/// 对图像进行水平方向倾斜变换,基准点为平行四边形左上顶点
/// </summary>
/// <param name="b">原始图像</param>
/// <param name="horz">水平方向倾斜量</param>
/// <returns></returns>
public Bitmap SlantHorz(Bitmap b, int horz)
{
int width = b.Width;
int height = b.Height;
Bitmap dstImage = new Bitmap(Math.Abs(horz) + width, height);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dstImage);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
Point[] dstPoints = new Point[3];
// 分别计算平行四边形的左上、右上和左下三个顶点
if (horz != 0)
{
if (horz > 0)
{
dstPoints[0] = new Point( horz, 0);
dstPoints[1] = new Point(width + horz, 0);
dstPoints[2] = new Point( 0, height);
}
else
{
dstPoints[0] = new Point( 0, 0);
dstPoints[1] = new Point(width, 0);
dstPoints[2] = new Point(-horz, height);
}
}
// 绘水平倾斜图
g.DrawImage(b, dstPoints);
g.Save();
g.Dispose();
b.Dispose();
return dstImage;
} // end of SlantHorz
/// <summary>
/// 对图像进行垂直方向倾斜变换,基准点为平行四边形左上顶点
/// </summary>
/// <param name="b">原始图像</param>
/// <param name="vert">垂直方向倾斜量</param>
/// <returns></returns>
public Bitmap SlantVert(Bitmap b, int vert)
{
int width = b.Width;
int height = b.Height;
Bitmap dstImage = new Bitmap(width, Math.Abs(vert) + height);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dstImage);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
Point[] dstPoints = new Point[3];
// 分别计算平行四边形的左上、右上和左下三个顶点
if (vert != 0)
{
if (vert > 0)
{
dstPoints[0] = new Point( 0, vert);
dstPoints[1] = new Point(width, 0);
dstPoints[2] = new Point( 0, height + vert);
}
else
{
dstPoints[0] = new Point( 0, 0);
dstPoints[1] = new Point(width, -vert);
dstPoints[2] = new Point( 0, height);
}
}
// 绘水平倾斜图
g.DrawImage(b, dstPoints);
g.Save();
g.Dispose();
b.Dispose();
return dstImage;
} // end of SlantVert
/// <summary>
/// 修整
/// </summary>
/// <param name="b">位图流</param>
/// <param name="trimAway">修整范围</param>
/// <returns></returns>
public Bitmap Trim(Bitmap b, TrimMode trimAway)
{
int width = b.Width;
int height = b.Height;
// 原始图像大小
int area = width * height;
BitmapData data = b.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
int stride = data.Stride;
System.IntPtr scan0 = data.Scan0;
int offset = stride - width * BPP;
// 修整后的有效区域
int rectTop = 0;
int rectBottom = height;
int rectLeft = 0;
int rectRight = width;
unsafe
{
byte* p = (byte*)scan0;
int i = 1;
// 上
if ((trimAway & TrimMode.Top) == TrimMode.Top)
{
p = (byte*)scan0;
i = 1;
while (i < area)
{
if (p[3] != 0)
{
// 获取上边界
rectTop = i / width;
break;
}
p += BPP;
if (i % width == 0)
p += offset;
i++;
} // while
}
// 下
if ((trimAway & TrimMode.Bottom) == TrimMode.Bottom)
{
p = (byte*)scan0 + (height - 1) * stride + (width - 1) * BPP;
i = 1;
while (i < area)
{
if (p[3] != 0)
{
// 获取下边界
rectBottom = height - i / width;
break;
}
p -= BPP;
if (i % width == 0)
p -= offset;
i++;
} // while
}
// 左
if ((trimAway & TrimMode.Left) == TrimMode.Left)
{
p = (byte*)scan0;
i = 1;
while (i < area)
{
if (p[3] != 0)
{
// 获取左边界
rectLeft = i / height;
break;
}
p += stride;
if (i % height == 0)
{
p = (byte*)scan0;
p += (i / height) * BPP;
}
i++;
} // while
}
// 右
if ((trimAway & TrimMode.Right) == TrimMode.Right)
{
p = (byte*)scan0 + (width - 1) * BPP;
i = 1;
while (i < area)
{
if (p[3] != 0)
{
// 获取右边界
rectRight = width - i / height;
break;
}
p += stride;
if (i % height == 0)
{
p = (byte*)scan0 + (width - 1) * BPP;
p -= (i / height) * BPP;
}
i++;
} // while
}
}
b.UnlockBits(data);
// 修整有效区域后的图像
Bitmap dst = new Bitmap(rectRight - rectLeft, rectBottom - rectTop);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dst);
g.DrawImage(b,
new Rectangle(0, 0, dst.Width, dst.Height),
new Rectangle(rectLeft, rectTop, dst.Width, dst.Height),
GraphicsUnit.Pixel
);
g.Save();
b.Dispose();
return dst;
} // end of Trim
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -