[C#] 纯文本查看 复制代码
public class ImageControl
{
/// <summary>
/// 梯形方向枚举
/// </summary>
public enum TrapezoidDerection
{
/// <summary>
/// 从上到下(上短下长)
/// </summary>
UpperToLower = 0,
/// <summary>
/// 从下到上(下短上长)
/// </summary>
LowertOUpper = 1,
/// <summary>
/// 从左到右(左短右长)
/// </summary>
LeftToRight = 2,
/// <summary>
/// 从右到左(右短左长)
/// </summary>
RightToLeft = 3
}
/// <summary>
/// 图片变换成梯形(用于制作3D翻转效果)
/// </summary>
/// <param name="srcBitmap">原始图片</param>
/// <param name="trapezoidDerection">梯形绘制方向(详细查看枚举TrapezoidDerection)</param>
/// <param name="trapezoidTanValue">三角函数正切值Tan(度数):用于计算梯形短边的缩进大小</param>
/// <returns></returns>
public static Bitmap BitmapTrapezoid(Bitmap srcBitmap, TrapezoidDerection trapezoidDerection, double trapezoidTanValue)
{
Bitmap btm = new Bitmap(srcBitmap.Width, srcBitmap.Height);
Graphics g = Graphics.FromImage(btm);
g.Clear(Color.Transparent); //初始为透明色
int tanh = 0;
//逐行读取图片并转换成梯形
switch (trapezoidDerection)
{
case TrapezoidDerection.UpperToLower:
#region 上到下绘制梯形方向
tanh = srcBitmap.Height;
for (int i = 0; i < srcBitmap.Height; i++)
{
//函数tan(10)*邻边=需要缩放的宽度
double delwidth = trapezoidTanValue * tanh;
using (Bitmap bmpOut = new Bitmap(srcBitmap.Width, 1))
{
Graphics gbmpOut = Graphics.FromImage(bmpOut);
gbmpOut.Clear(Color.Transparent);
//获取高度在i的位置上的一个像素图片,并缩放
gbmpOut.DrawImage(srcBitmap,
new Rectangle((int)delwidth, 0, srcBitmap.Width - (int)delwidth - (int)delwidth, 1),
new Rectangle(0, i, srcBitmap.Width, 1), GraphicsUnit.Pixel);
//绘制到背景图片中
g.DrawImage(bmpOut, new Point(0, i));
}
tanh--;
}
#endregion
break;
case TrapezoidDerection.LowertOUpper:
#region 下到上绘制梯形方向
tanh = srcBitmap.Height;
for (int i = srcBitmap.Height - 1; i >= 0; i--)
{
//函数tan(10)*邻边=需要缩放的宽度
double delwidth = trapezoidTanValue * (tanh);
using (Bitmap bmpOut = new Bitmap(srcBitmap.Width, 1))
{
Graphics gbmpOut = Graphics.FromImage(bmpOut);
gbmpOut.Clear(Color.Transparent);
//获取高度在i的位置上的一个像素图片,并缩放
gbmpOut.DrawImage(srcBitmap,
new Rectangle((int)delwidth, 0, srcBitmap.Width - (int)delwidth - (int)delwidth, 1),
new Rectangle(0, i, srcBitmap.Width, 1), GraphicsUnit.Pixel);
//绘制到背景图片中
g.DrawImage(bmpOut, new Point(0, i));
}
tanh--;
}
#endregion
break;
case TrapezoidDerection.LeftToRight:
#region 左到右绘制梯形方向
//待优化,水平变换有点卡
//tanh = srcBitmap.Width;
//for (int i = 0; i < 140; i++)
//{
// //函数tan(10)*邻边=需要缩放的宽度
// double delwidth = trapezoidTanValue * tanh;
// using (Bitmap bmpOut = new Bitmap(20, srcBitmap.Height))
// {
// Graphics gbmpOut = Graphics.FromImage(bmpOut);
// gbmpOut.Clear(Color.Transparent);
// //获取宽度度在i的位置上的一个像素图片,并缩放
// gbmpOut.DrawImage(srcBitmap,
// new Rectangle(0, 2, 1, srcBitmap.Height),
// new Rectangle(i, 0, 1, srcBitmap.Height), GraphicsUnit.Pixel);
// //绘制到背景图片中
// g.DrawImage(bmpOut, new Point(i, 0));
// }
// tanh--;
//}
#endregion
break;
case TrapezoidDerection.RightToLeft:
#region 右到左绘制梯形方向
//tanh = srcBitmap.Width;
//for (int i = srcBitmap.Width - 1; i >= 0; i--)
//{
// //函数tan(10)*邻边=需要缩放的高度
// double delheight = trapezoidTanValue * tanh;
// //double delheight = (1.0 - trapezoidAngle) * srcBitmap.Height / 2 / srcBitmap.Width;
// using (Bitmap bmpOut = new Bitmap(1, srcBitmap.Height))
// {
// Graphics gbmpOut = Graphics.FromImage(bmpOut);
// gbmpOut.Clear(Color.Transparent);
// //获取宽度度在i的位置上的一个像素图片,并缩放
// gbmpOut.DrawImage(srcBitmap,
// new Rectangle(0, (int)delheight, 1, srcBitmap.Height - (int)delheight - (int)delheight),
// new Rectangle(i, 0, 1, srcBitmap.Height), GraphicsUnit.Pixel);
// //绘制到背景图片中
// g.DrawImage(bmpOut, new Point(i, 0));
// }
// tanh--;
//}
#endregion
break;
}
g.Dispose();
return btm;
}
/// <summary>
/// 垂直缩放图像
/// </summary>
/// <param name="srcBitmap">原始图片</param>
/// <param name="changedHeight">将图像分割为上下两个部分,此值代表其中一半图像缩放后的高度</param>
/// <returns></returns>
public static Bitmap ImageRotateFlipY(Bitmap srcBitmap, int changedHeight)
{
try
{
int width = srcBitmap.Width; //图像宽度
int height = srcBitmap.Height; //图像高度
//创建新位图
Bitmap bmp = new Bitmap(width, height);
//创建新画布
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.Transparent); //初始为透明色
//以图像中间位置为基线,达到上下同时向中心缩放效果
Rectangle DestRect = new Rectangle(0, height / 2 - changedHeight, width, 2 * changedHeight);
Rectangle SrcRect = new Rectangle(0, 0, srcBitmap.Width, srcBitmap.Height);
g.DrawImage(srcBitmap, DestRect, SrcRect, GraphicsUnit.Pixel);
return bmp;
}
catch
{
return null;
}
}
/// <summary>
/// 水平缩放图像
/// </summary>
/// <param name="srcBitmap">原始图片</param>
/// <param name="changedWidth">将图像分割为左右两个部分,此值代表其中一半图像缩放后的宽度度</param>
/// <returns></returns>
public static Bitmap ImageRotateFlipX(Bitmap srcBitmap, int changedWidth)
{
try
{
int width = srcBitmap.Width; //图像宽度
int height = srcBitmap.Height; //图像高度
//创建新位图
Bitmap bmp = new Bitmap(width, height);
//创建新画布
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.Transparent); //初始为透明色
Rectangle DestRect = new Rectangle(width / 2 - changedWidth, 0, 2 * changedWidth, height);
Rectangle SrcRect = new Rectangle(0, 0, srcBitmap.Width, srcBitmap.Height);
g.DrawImage(srcBitmap, DestRect, SrcRect, GraphicsUnit.Pixel);
GC.Collect(0);
return bmp;
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message, "信息提示");
return null;
}
}
}