public static Bitmap generate(Bitmap src, string str) { src = ImgPreprocessor.Resize(src); Bitmap res = new Bitmap(src.Width * Configue.FONT_WIDTH, src.Height * Configue.FONT_WIDTH); Graphics g = Graphics.FromImage(res); g.Clear(Color.Black); Font font = new Font("微软雅黑", Configue.FONT_WIDTH * 3 / 4); BitmapData bitmapData = src.LockBits(new Rectangle(0, 0, src.Width, src.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); int index = 0, len = str.Length; unsafe { byte *ptr = (byte *)bitmapData.Scan0; for (int j = 0; j < bitmapData.Height; j++) { for (int i = 0; i < bitmapData.Width; i++) { Color color = Color.FromArgb(ptr[2], ptr[1], ptr[0]); Brush brush = new SolidBrush(color); g.DrawString(str[index].ToString(), font, brush, i * Configue.FONT_WIDTH, j * Configue.FONT_WIDTH); index = (index + 1) % len; ptr += 3; } ptr += bitmapData.Stride - (bitmapData.Width * 3); String log = String.Format("Line:{0} Finish", j); Console.WriteLine(log); } } return(res); }
public static Bitmap Combine(Bitmap src) { src = ImgPreprocessor.Resize(src); Bitmap defaultImage = (Bitmap)Image.FromFile(Configue.DEFAULT_IMG); BitmapData bitmapData = src.LockBits(new Rectangle(0, 0, src.Width, src.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); Bitmap res = new Bitmap(src.Width * Configue.SLICE_WIDTH, src.Height * Configue.SLICE_HEIGHT); Graphics g = Graphics.FromImage(res); unsafe { byte *ptr = (byte *)bitmapData.Scan0; for (int j = 0; j < src.Height; j++) { for (int i = 0; i < src.Width; i++) { Bitmap slice = getSimiliarColorImg(ptr[2], ptr[1], ptr[0]); if (slice == null) { slice = defaultImage; } g.DrawImage(slice, i * Configue.SLICE_WIDTH, j * Configue.SLICE_HEIGHT); ptr += 3; } String log = String.Format("Line:{0} Finish", j); Console.WriteLine(log); ptr += bitmapData.Stride - (bitmapData.Width * 3); } } return(res); }
static void Main(string[] args) { //运行这一段 预处理样本图片 ImgPreprocessor.process(); //运行这一段 生成拼图 Bitmap src = (Bitmap)Image.FromFile("12.jpg"); Bitmap res = Combiner.Combine(src); res.Save("res.jpg"); //运行这一段 生成字体拼图 //Bitmap src = (Bitmap)Image.FromFile("1.jpg"); //Bitmap res = FontImage.generate(src, "我爱你"); //res.Save("res2.jpg"); }