大家好,小范来为大家解答以上的问题。使透明这个很多人还不知道,现在让我们一起来看看吧!
1、正好软糖做过这个软件^o^,切割图片并保存一堆小图片到FileName-###.png效果图软糖切图 - 核心代码using System;using System.Drawing;using System.Drawing.Imaging;using System.Drawing.Drawing2D;using System.IO;namespace 软糖切图{ public class 网格精灵 { public string 文件地址; public string 输出目录; public string 输出后缀名 = @".png"; public static string 命名格式 = @"{0}_{1}_{2}"; public Bitmap 原始; public Bitmap 当前; public ImageFormat 图像格式; public Graphics 画板; public Color 透明色; public Pen 网格画笔 = new Pen(Color.FromArgb(255, 255, 0, 255), 2); public Color 无效区域色 = Color.White; public Brush 背景画刷 = new HatchBrush(HatchStyle.LightDownwardDiagonal, Color.White,Color.LightGray); public int OX = 0; public int OY = 0; public int W; public int H; public int iX; public int iY; public bool 显示网格 = true; public void 打开(string 文件地址) { 装载位图( new Bitmap(文件地址)); } public void 装载位图(Bitmap 位图) { 原始 = 位图; 图像格式 = ImageFormat.Png; 自动调整网格(); 当前 = new Bitmap(原始, 原始.Size); 画板 = Graphics.FromImage(当前); } public void 重新装载() { 当前 = new Bitmap(原始, 原始.Size); 画板 = Graphics.FromImage(当前); } public void 重新绘制() { if (原始 == null) { return; } 绘制位图(画板); 绘制网格(画板); } public void 调整网格线宽度(float 宽度) { if (宽度 <= 0) { 显示网格 = false; } else { 显示网格 = true; } 网格画笔.Width = 宽度; 重新绘制(); } private void 自动调整网格() { if (原始.Width < W) { W = 原始.Width; } if (原始.Height < H) { H = 原始.Height; } //计算格子数量 iX = (int)((原始.Width - OX) / W); iY = (int)((原始.Height - OY) / H); } private void 绘制位图(Graphics v画板) { v画板.Clear(无效区域色); if (当前 == null) { return; } Rectangle RECT = new Rectangle(0, 0, 当前.Width, 当前.Height); v画板.FillRectangle(背景画刷, RECT); v画板.DrawImage(原始, RECT); } private void 绘制网格(Graphics v画板) { //检查位图和格子宽高 if (当前 == null) { return; } if (显示网格 == false) { return; } if (W <= 0) { W = 32; } if (H <= 0) { H = 32; } //计算格子数量 iX = (int)((当前.Width - OX) / W); iY = (int)((当前.Height - OY) / H); //检查格子数量 if (iX < 1) { return; } if (iY < 1) { return; } //绘制网格线 int x1, x2, y1, y2; for (int x = 1; x <= iX + 1; x++) { x1 = (x - 1) * W - OX; y1 = 0; x2 = x1; y2 = 当前.Height; v画板.DrawLine(网格画笔, x1, y1, x2, y2); } for (int y = 1; y <= iY + 1; y++) { x1 = 0; y1 = (y - 1) * H - OY; x2 = 当前.Width; y2 = y1; v画板.DrawLine(网格画笔, x1, y1, x2, y2); } } public Color 取色(int x,int y) { if (当前 == null){ return Color.Black; } return 当前.GetPixel(x, y); } public void 开关透明(bool 透明) { if (当前 == null) { return; } //重新装载(); if (透明) { 当前.MakeTransparent(透明色); } 重新绘制(); } public void 裁剪() { //检查位图和格子宽高 if (当前 == null) { return; } if (W <= 0) { W = 32; } if (H <= 0) { H = 32; } //计算格子数量 iX = (int)((当前.Width - OX) / W); iY = (int)((当前.Height - OY) / H); //检查格子数量 if (iX <= 1) { return; } if (iY <= 1) { return; } //剪辑位图并保存 for (int y = 1; y <= iY; y++) { for (int x = 1; x <= iX; x++) { int i = x + (y - 1) * iX; Rectangle 源矩形 = new Rectangle( (x - 1) * W - OX, (y - 1) * H - OY, W, H); Rectangle 目标矩形 = new Rectangle(0, 0, W, H); Bitmap t剪辑 = new Bitmap(W, H, PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(t剪辑); g.DrawImage(当前, 目标矩形, 源矩形, GraphicsUnit.Pixel); g.Dispose(); string 路径 = Path.Combine(输出目录, 获取文件名(i) + 获取后缀名()); t剪辑.Save(路径, 图像格式); } } } private string 获取文件名(int ID) { string 文件名 = System.IO.Path.GetFileNameWithoutExtension(文件地址); string strID = ID.ToString(); string strDate = DateTime.Now.ToString("yyyyMMdd"); return string.Format(命名格式, 文件名, strID, strDate); } private string 获取后缀名() { return 输出后缀名; } }}调用方法精灵.打开(文件地址);精灵.W = 32; //每个分割方块的宽度精灵.H = 32; //每个分割方块的高度//精灵.透明色 = Color.FromArgb(255,255,255); //需要透明色就删掉注释//精灵.开关透明 = true;精灵.输出目录 = @"D:Out";精灵.裁剪();比如你的图片是50*50的,要分割成4张25*25的首先初始化4张bitmap。
2、然后遍历50*50的bitmap,利用Getpixel获取像素点,然后Setpixel给25*25的图片。
本文到此分享完毕,希望对大家有所帮助。