C# 模仿微信生成九宮圖頭像

ChenChen發表於2017-09-26

效果圖
test1test2test3test4test5test6test7test8test9


.NET Core 需要安裝 ZKWeb.System.Drawing 包
ZKWeb.System.Drawing

引用

using System.DrawingCore;
using System.DrawingCore.Drawing2D;
using System.IO;
using System.Net;

九宮圖

public class JiuGongDiagram {
    private const int _width = 132; //合成九宮圖的寬度
    private const int _height = 132; //合成九宮圖的高度

    /// <summary>
    /// 獲取座標
    /// </summary>
    /// <param name="number">數量</param>
    /// <returns></returns>
    private string[] GetXy(int number) {
        string[] s = new string[number];
        int _x = 0;
        int _y = 0;
        switch (number) {
            case 1:
                _x = _y = 6;
                s[0] = "6,6";
                break;
            case 2:
                _x = _y = 4;
                s[0] = "4," + (132 / 2 - 60 / 2);
                s[1] = 60 + 2 * _x + "," + (132 / 2 - 60 / 2);
                break;
            case 3:
                _x = _y = 4;
                s[0] = (132 / 2 - 60 / 2) + "," + _y;
                s[1] = _x + "," + (60 + 2 * _y);
                s[2] = (60 + 2 * _y) + "," + (60 + 2 * _y);
                break;
            case 4:
                _x = _y = 4;
                s[0] = _x + "," + _y;
                s[1] = (_x * 2 + 60) + "," + _y;
                s[2] = _x + "," + (60 + 2 * _y);
                s[3] = (60 + 2 * _y) + "," + (60 + 2 * _y);
                break;
            case 5:
                _x = _y = 3;
                s[0] = (132 - 40 * 2 - _x) / 2 + "," + (132 - 40 * 2 - _y) / 2;
                s[1] = ((132 - 40 * 2 - _x) / 2 + 40 + _x) + "," + (132 - 40 * 2 - _y) / 2;
                s[2] = _x + "," + ((132 - 40 * 2 - _x) / 2 + 40 + _y);
                s[3] = (_x * 2 + 40) + "," + ((132 - 40 * 2 - _x) / 2 + 40 + _y);
                s[4] = (_x * 3 + 40 * 2) + "," + ((132 - 40 * 2 - _x) / 2 + 40 + _y);
                break;
            case 6:
                _x = _y = 3;
                s[0] = _x + "," + ((132 - 40 * 2 - _x) / 2);
                s[1] = (_x * 2 + 40) + "," + ((132 - 40 * 2 - _x) / 2);
                s[2] = (_x * 3 + 40 * 2) + "," + ((132 - 40 * 2 - _x) / 2);
                s[3] = _x + "," + ((132 - 40 * 2 - _x) / 2 + 40 + _y);
                s[4] = (_x * 2 + 40) + "," + ((132 - 40 * 2 - _x) / 2 + 40 + _y);
                s[5] = (_x * 3 + 40 * 2) + "," + ((132 - 40 * 2 - _x) / 2 + 40 + _y);
                break;
            case 7:
                _x = _y = 3;
                s[0] = (132 - 40) / 2 + "," + _y;
                s[1] = _x + "," + (_y * 2 + 40);
                s[2] = (_x * 2 + 40) + "," + (_y * 2 + 40);
                s[3] = (_x * 3 + 40 * 2) + "," + (_y * 2 + 40);
                s[4] = _x + "," + (_y * 3 + 40 * 2);
                s[5] = (_x * 2 + 40) + "," + (_y * 3 + 40 * 2);
                s[6] = (_x * 3 + 40 * 2) + "," + (_y * 3 + 40 * 2);
                break;
            case 8:
                _x = _y = 3;
                s[0] = (132 - 80 - _x) / 2 + "," + _y;
                s[1] = ((132 - 80 - _x) / 2 + _x + 40) + "," + _y;
                s[2] = _x + "," + (_y * 2 + 40);
                s[3] = (_x * 2 + 40) + "," + (_y * 2 + 40);
                s[4] = (_x * 3 + 40 * 2) + "," + (_y * 2 + 40);
                s[5] = _x + "," + (_y * 3 + 40 * 2);
                s[6] = (_x * 2 + 40) + "," + (_y * 3 + 40 * 2);
                s[7] = (_x * 3 + 40 * 2) + "," + (_y * 3 + 40 * 2);
                break;
            case 9:
                _x = _y = 3;
                s[0] = _x + "," + _y;
                s[1] = _x * 2 + 40 + "," + _y;
                s[2] = _x * 3 + 40 * 2 + "," + _y;
                s[3] = _x + "," + (_y * 2 + 40);
                s[4] = (_x * 2 + 40) + "," + (_y * 2 + 40);
                s[5] = (_x * 3 + 40 * 2) + "," + (_y * 2 + 40);
                s[6] = _x + "," + (_y * 3 + 40 * 2);
                s[7] = (_x * 2 + 40) + "," + (_y * 3 + 40 * 2);
                s[8] = (_x * 3 + 40 * 2) + "," + (_y * 3 + 40 * 2);
                break;
        }
        return s;
    }

    /// <summary>
    /// 獲取尺寸
    /// </summary>
    /// <param name="number">數量</param>
    /// <returns></returns>
    private float GetSize(int number) {
        int w = 0;
        if (number == 1) {
            w = 120;
        }
        if (number > 1 && number <= 4) {
            w = 60;
        }
        if (number >= 5) {
            w = 40;
        }
        return w;
    }

    /// <summary>
    /// 圖片壓縮
    /// </summary>
    /// <param name="bitmap">Bitmap</param>
    /// <param name="width"></param>
    /// <param name="height"></param>
    /// <returns></returns>
    private Bitmap ImageCompression(Bitmap bitmap, int width, int height) {
        using(var bmp = new Bitmap(width, height)) {
            //從Bitmap建立一個Graphics物件,用來繪製高質量的縮小圖。  
            using(var gr = Graphics.FromImage(bmp)) {
                //設定 System.Drawing.Graphics物件的SmoothingMode屬性為HighQuality  
                gr.SmoothingMode = SmoothingMode.HighQuality;
                //下面這個也設成高質量  
                gr.CompositingQuality = CompositingQuality.HighQuality;
                //下面這個設成High  
                gr.InterpolationMode = InterpolationMode.High;
                //把原始影象繪製成上面所設定寬高的擷取圖  
                var rectDestination = new Rectangle(0, 0, width, height);
                //繪製
                gr.DrawImage(bitmap, 0, 0, rectDestination, GraphicsUnit.Pixel);
                //儲存擷取的圖片  
                return bitmap;
            }
        }
    }

    /// <summary>
    /// 下載圖片
    /// </summary>
    /// <param name="path">路徑</param>
    /// <returns></returns>
    private Bitmap PathConvertBitmap(string path, bool isHttp) {
        if (!isHttp) {
            return new Bitmap(path);
        }
        using(WebClient client = new WebClient()) {
            using(Stream stream = client.OpenRead(path)) {
                return (Bitmap) Image.FromStream(stream);
            }
        }
    }

    /// <summary>
    /// 合成九宮圖
    /// </summary>
    /// <param name="imgPath">圖片路徑(絕對路徑)</param>
    /// <param name="isHttp">網路圖片</param>
    /// <returns></returns>
    public Bitmap Synthetic(string[] imgPath, bool isHttp = false) {
        //合成數量
        int number = imgPath.Length;
        //座標
        string[] xys = this.GetXy(number);
        //尺寸
        var size = this.GetSize(number);
        Bitmap backgroudBitmap = new Bitmap(_width, _height);
        using(Graphics g = Graphics.FromImage(backgroudBitmap)) {
            //設定 System.Drawing.Graphics物件的SmoothingMode屬性為HighQuality  
            g.SmoothingMode = SmoothingMode.HighQuality;
            //下面這個也設成高質量  
            g.CompositingQuality = CompositingQuality.HighQuality;
            //下面這個設成High  
            g.InterpolationMode = InterpolationMode.High;
            //清除畫布,背景設定為透明色
            g.Clear(Color.FromArgb(255, 255, 255));
            for (int i = 0; i < imgPath.Length; i++) {
                var bitmap = this.PathConvertBitmap(imgPath[i], isHttp);
                string[] xy = xys[i].Split(',');
                int x = int.Parse(xy[0]);
                int y = int.Parse(xy[1]);
                bitmap = this.ImageCompression(bitmap, x, y);
                g.DrawImage(bitmap, x, y, size, size);
                bitmap.Dispose();
            }
        }
        return backgroudBitmap;
    }
}

本地圖片合成九宮圖

//絕對路徑
string[] path = new string[] {
    @"D:\test\7.jpg",
    @"D:\test\6.jpg", 
    @"D:\test\5.jpg"
};
JiuGongDiagram jiuGong = new JiuGongDiagram();
var bitmap = jiuGong.Synthetic(path);
//可以儲存到本地或者上傳到檔案伺服器
bitmap.Save(@"D:\test\test.jpg", System.DrawingCore.Imaging.ImageFormat.Png);
bitmap.Dispose();

下載網路圖片合成九宮圖

//網路路徑
string[] path = new string[] {
    "http://www.qqzhi.com/uploadpic/2014-09-12/094036231.jpg",
    "http://www.qqzhi.com/uploadpic/2014-09-12/094036231.jpg", 
    "http://www.qqzhi.com/uploadpic/2014-09-12/094036231.jpg"
};
JiuGongDiagram jiuGong = new JiuGongDiagram();
var bitmap = jiuGong.Synthetic(path,true);
//可以儲存到本地或者上傳到檔案伺服器
bitmap.Save(@"D:\test\test.jpg", System.DrawingCore.Imaging.ImageFormat.Png);
bitmap.Dispose();

相關文章