一、遇到的问题
在做项目的时候遇到一个需要局部拉伸图片的问题
比如:1.对话框背景 在文字很长多行显示的时候 如果直接将背景图片平铺 会造成变形影响美观
2.圆形背景 显示内容很长时 选择平铺 也会造成变形
下面为直接平铺效果 也就是 Stretch
二、想要的效果
最终我们要实现的效果
三、思路
1.将原图按田字格平均分为四部分
2.确定拉伸后的图片大小
3.将四个部分按像素分别画到新图片的四个角落上,对中间部分进行填充
4.显示新图片
四、主要用到的方法:
//
// 摘要:
// 在指定位置并且按指定大小绘制指定的 System.Drawing.Image 的指定部分。
//
// 参数:
// image:
// 要绘制的 System.Drawing.Image。
//
// destRect:
// System.Drawing.Rectangle 结构,它指定所绘制图像的位置和大小。 将图像进行缩放以适合该矩形。
//
// srcX:
// 要绘制的源图像部分的左上角的 x 坐标。
//
// srcY:
// 要绘制的源图像部分的左上角的 y 坐标。
//
// srcWidth:
// 要绘制的源图像部分的宽度。
//
// srcHeight:
// 要绘制的源图像部分的高度。
//
// srcUnit:
// System.Drawing.GraphicsUnit 枚举的成员,它指定用于确定源矩形的度量单位。
//
// 异常:
// T:System.ArgumentNullException:
// image 为 null。
public void DrawImage(Image image, Rectangle destRect, int srcX, int srcY, int srcWidth, int srcHeight, GraphicsUnit srcUnit);
五、代码
1.定义结构体 指定图片哪些部分
public struct EdgeInset
{
/// <summary>
/// 上边距
/// </summary>
public int Top { set; get; }
/// <summary>
/// 左边距
/// </summary>
public int Left { get; set; }
/// <summary>
/// 下边距
/// </summary>
public int Bottom { get; set; }
/// <summary>
/// 右边距
/// </summary>
public int Right { get; set; }
public EdgeInset(int top, int left, int bottom, int right)
{
Top = top;
Left = left;
Bottom = bottom;
Right = right;
}
}
2.局部拉伸
EdgeInset Edge = new EdgeInset(8,8,8,8); //设置需要拉伸的部分,可以根据自己需要设置
public Form1()
{
InitializeComponent();
}
private void btnStretch_Click(object sender, EventArgs e)
{
Image Image= pictureBox1.Image;
Image bgImage = new Bitmap(this.picStretch.Width, this.picStretch.Height);
Graphics gp = Graphics.FromImage(bgImage);
gp.DrawImage(Image, new Rectangle(0, 0, Edge.Left, Edge.Top), 0, 0, Edge.Left, Edge.Top, GraphicsUnit.Pixel);//左上
gp.DrawImage(Image, new Rectangle(bgImage.Width - Edge.Right, 0, Edge.Right, Edge.Top), Image.Width - Edge.Right, 0, Edge.Right, Edge.Top, GraphicsUnit.Pixel);//右上
gp.DrawImage(Image, new Rectangle(0, bgImage.Height - Edge.Bottom, Edge.Left, Edge.Bottom), 0, Image.Height - Edge.Bottom, Edge.Left, Edge.Bottom, GraphicsUnit.Pixel); //左下
gp.DrawImage(Image, new Rectangle(bgImage.Width - Edge.Right, bgImage.Height - Edge.Bottom, Edge.Right, Edge.Bottom), Image.Width - Edge.Right, Image.Height - Edge.Bottom, Edge.Right, Edge.Bottom, GraphicsUnit.Pixel); //右下
int iTemp = Image.Width - Edge.Left - Edge.Right;
iTemp = iTemp > 0 ? iTemp : 1;
gp.DrawImage(Image, new Rectangle(Edge.Left, 0, bgImage.Width - Edge.Right - Edge.Left, Edge.Top), Edge.Right, 0, iTemp, Edge.Top, GraphicsUnit.Pixel);//中上
gp.DrawImage(Image, new Rectangle(Edge.Left, bgImage.Height - Edge.Bottom, bgImage.Width - Edge.Right - Edge.Left, Edge.Top), Edge.Right, Image.Height - Edge.Bottom, iTemp, Edge.Top, GraphicsUnit.Pixel);//中下
iTemp = Image.Height - Edge.Bottom - Edge.Top;
iTemp = iTemp > 0 ? iTemp : 1;
gp.DrawImage(Image, new Rectangle(0, Edge.Top, Edge.Left, bgImage.Height - Edge.Top - Edge.Bottom), 0, Edge.Top, Edge.Left, iTemp, GraphicsUnit.Pixel);//左中
gp.DrawImage(Image, new Rectangle(bgImage.Width - Edge.Right, Edge.Top, Edge.Right, bgImage.Height - Edge.Top - Edge.Bottom), Image.Width - Edge.Right, Edge.Top, Edge.Right, iTemp, GraphicsUnit.Pixel);//右中
iTemp = Image.Width - Edge.Left - Edge.Right;
iTemp = iTemp > 0 ? iTemp : 1; //width
int iTemp1 = Image.Height - Edge.Top - Edge.Bottom;
iTemp1 = iTemp1 > 0 ? iTemp1 : 1;
gp.DrawImage(Image, new Rectangle(Edge.Left, Edge.Top, bgImage.Width - Edge.Left - Edge.Right, bgImage.Height - Edge.Top - Edge.Bottom), Edge.Left, Edge.Top, iTemp, iTemp1, GraphicsUnit.Pixel);//中间
this.picStretch.Image = bgImage;
}