C#切割图片自动填充工具栏按钮图片
C#切割图片自动填充工具栏按钮图片
多多动手,什么都简单了。
窗体的代码:
多多动手,什么都简单了。
C# Code:
/// <summary>
/// 图像处理辅助类
/// </summary>
public class ImageHelper
{
/// <summary>
/// 获取均分图片中的某一个
/// </summary>
public static Image GetImageByAverageIndex(Image orignal, int count, int index)
{
int width = orignal.Width / count;
return CutImage(orignal, width * (index - 1), width, orignal.Height);
}
/// <summary>
/// 获取图片一部分
/// </summary>
private static Image CutImage(Image orignal, int start, int width, int height)
{
Bitmap partImage = new Bitmap(width, height);
Graphics g = Graphics.FromImage(partImage);//获取画板
Rectangle srcRect = new Rectangle(start, 0, width, height);//源位置开始
Rectangle destRect = new Rectangle(0, 0, width, height);//目标位置
//复制图片
g.DrawImage(orignal, destRect, srcRect, GraphicsUnit.Pixel);
partImage.MakeTransparent(Color.FromArgb(255, 0, 255));
g.Dispose();
return partImage;
}
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
/// <summary>
/// 图像处理辅助类
/// </summary>
public class ImageHelper
{
/// <summary>
/// 获取均分图片中的某一个
/// </summary>
public static Image GetImageByAverageIndex(Image orignal, int count, int index)
{
int width = orignal.Width / count;
return CutImage(orignal, width * (index - 1), width, orignal.Height);
}
/// <summary>
/// 获取图片一部分
/// </summary>
private static Image CutImage(Image orignal, int start, int width, int height)
{
Bitmap partImage = new Bitmap(width, height);
Graphics g = Graphics.FromImage(partImage);//获取画板
Rectangle srcRect = new Rectangle(start, 0, width, height);//源位置开始
Rectangle destRect = new Rectangle(0, 0, width, height);//目标位置
//复制图片
g.DrawImage(orignal, destRect, srcRect, GraphicsUnit.Pixel);
partImage.MakeTransparent(Color.FromArgb(255, 0, 255));
g.Dispose();
return partImage;
}
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
窗体的代码:
C# Code:
public partial class frmCutImage : Form
{
const int ICO_COUNT = 14;//小图片数量
public frmCutImage()
{
InitializeComponent();
}
private void btnCutImage_Click(object sender, EventArgs e)
{
//按图片所在位置截取图片
Image img = ImageHelper.GetImageByAverageIndex(pictureBox1.Image, ICO_COUNT, int.Parse(textBox1.Text));
pictureBox2.Image = img;
}
private void btnLoadToolbar_Click(object sender, EventArgs e)
{
Image img;
//生成14个按钮图片
for (int i = 1; i <= ICO_COUNT; i )
{
img = ImageHelper.GetImageByAverageIndex(pictureBox1.Image, ICO_COUNT, i);
this.toolStrip1.Items.Add(img);
}
}
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
public partial class frmCutImage : Form
{
const int ICO_COUNT = 14;//小图片数量
public frmCutImage()
{
InitializeComponent();
}
private void btnCutImage_Click(object sender, EventArgs e)
{
//按图片所在位置截取图片
Image img = ImageHelper.GetImageByAverageIndex(pictureBox1.Image, ICO_COUNT, int.Parse(textBox1.Text));
pictureBox2.Image = img;
}
private void btnLoadToolbar_Click(object sender, EventArgs e)
{
Image img;
//生成14个按钮图片
for (int i = 1; i <= ICO_COUNT; i )
{
img = ImageHelper.GetImageByAverageIndex(pictureBox1.Image, ICO_COUNT, i);
this.toolStrip1.Items.Add(img);
}
}
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网