//字符画
// 通过计算rowSize*colSize区域的亮度平均值用一个字符替代
        public static String BitmapConvert(Bitmap bitmap, int rowSize, int colSize)
        {
            StringBuilder result = new StringBuilder();
            char[] charset = { 'M', '8', '0', 'V', '1', 'i', ':', '*', '|', '.', ' '};
            int bitmapH = bitmap.Height;
            int bitmapW = bitmap.Width;
            for (int h = 0; h < bitmapH / rowSize; h++)
            {
                int offsetY = h * rowSize;
                for (int w = 0; w < bitmapW / colSize; w++)
                {
                    int offsetX = w * colSize;
                    float averBright = 0;
                    for (int j = 0; j < rowSize; j++)
                    {
                        for (int i = 0; i < colSize; i++)
                        {
                            try
                            {
                                Color color = bitmap.GetPixel(offsetX + i, offsetY + j);
                                averBright += color.GetBrightness();
                            }
                            catch (ArgumentOutOfRangeException)
                            {
                                averBright += 0;
                            }
                        }
                    }
                    averBright /= (rowSize * colSize);
                    int index = (int)(averBright * charset.Length);
                    if (index == charset.Length)
                        index--;
                    result.Append(charset[charset.Length - 1 - index]);
                }
                result.Append("\r\n");
            }
            return result.ToString();
        }




//拼接图片
// 获取一副图片的平均属性
        public static float getAverageProperty(Bitmap bitmap)
        {
            return getAveragePropertyInArea(bitmap, 0, 0, bitmap.Width, bitmap.Height);
        }
        
        /// <summary>
        /// 获取指定区域的平均属性
        /// </summary>
        /// <param name="bitmap">图片</param>
        /// <param name="startX">起点的x坐标</param>
        /// <param name="startY">起点的y坐标</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        /// <returns></returns>
        public static float getAveragePropertyInArea(Bitmap bitmap, int startX, int startY, int width, int height)
        {
            float averProperty = 0;
            for (int w = 0; w < width; w++)
            {
                for (int h = 0; h < height; h++)
                {
                    try
                    {
                        Color color = bitmap.GetPixel(startX + w, startY + h);
                        averProperty += color.GetBrightness();
                    }
                    catch (ArgumentOutOfRangeException)
                    {
                        averProperty += 0;
                    }
                }
            }
            return averProperty / (width * height);
        }
        
        /// <summary>
        /// 创建拼接图片
        /// </summary>
        /// <param name="target">需要生产的原图的路径</param>
        /// <param name="srcRoot">资源的图片文件夹路径</param>
        /// <param name="savePath">生产的图片保存路径</param>
        /// <param name="xSize"></param>
        /// <param name="ySize">和xSize决定原图的多大一块需要被图片替换</param>
        /// <param name="scale">缩放比例</param>
        public static void createImage(String target, String srcRoot, String savePath, int xSize, int ySize, int scale)
        {
            Bitmap srcTargetImg = new Bitmap(target);
            Bitmap saveImg = new Bitmap(srcTargetImg.Width * scale, srcTargetImg.Height * scale);
            
            Graphics g = Graphics.FromImage(saveImg);
            g.Clear(System.Drawing.Color.White);
            
            String[] files = System.IO.Directory.GetFiles(srcRoot);
            Hashtable table = new Hashtable();
            foreach (String file in files)
            {
                Bitmap bitmap = new Bitmap(file);
                if (bitmap != null)
                {
                    table.Add(file, getAverageProperty(bitmap));
                }
            }
            for (int w = 0; w < srcTargetImg.Width / xSize; w++)
            {
                for (int h = 0; h < srcTargetImg.Height / ySize; h++)
                {
                    float averProperty = getAveragePropertyInArea(srcTargetImg, w * xSize, h * ySize, xSize, ySize);
                    // 找到最接近的一张图
                    String fileToFind = "";
                    float lastDelta = 2;
                    foreach (DictionaryEntry de in table)
                    {
                        if (Math.Abs(averProperty - (float)de.Value) < lastDelta)
                        {
                            lastDelta = Math.Abs(averProperty - (float)de.Value);
                            fileToFind = (String)de.Key;
                        }
                    }
                    Bitmap tmp = new Bitmap(fileToFind);
                    // 绘制最终图片
                    g.DrawImage(tmp, w * xSize * scale, h * ySize * scale, xSize * scale, ySize * scale);
                }
            }
            g.Dispose();
            saveImg.Save(savePath);
        }
private void DrawLines(Graphics grp, List<Point> pointList)
        {
            Point one, two;
            for (int i = 0; i < pointList.Count-1; i++)
            {
                one = pointList[i];
                two = pointList[i + 1];
                grp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                grp.DrawLine(new Pen(Color.Red, 2), one,two);
            }
            grp.Dispose();
        }

 

 posted on 2015-11-13 13:29  C..J  阅读(574)  评论(0编辑  收藏  举报