C# 模仿微信生成九宮圖頭像
效果圖
.NET Core 需要安裝 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();
相關文章
- Flutter 仿微信/微博九宮格Flutter
- QQ群頭像 微信群頭像 多圖合併框架實現框架
- 擷取圖片生成頭像外掛
- C#上傳頭像剪裁,並生成多種尺寸C#
- C#使用OpenCV剪下圖片中的人物頭像C#OpenCV
- 微信服務號分享圖片(包括微信頭像以及二維碼)
- python九宮格圖片的原理Python
- 小程式—九宮格心形拼圖
- css 九宮格CSS
- 遊戲九宮格遊戲
- iOS 實現點選微信頭像效果iOS
- C#實現攝像頭呼叫、拍照C#
- 九宮八陣圖之天覆陣——防火牆防火牆
- 九宮格切圖-創意分享新風尚
- 基於 GD 庫生成圓形頭像
- PHP生成卡哇伊隨機頭像PHP隨機
- PHP+Ajax微信手機端九宮格抽獎例項PHP
- Kotlin 背景圓頭像圖Kotlin
- 北京冬奧會頭像一鍵生成原始碼原始碼
- UI Avatars 拿來即用的頭像生成ApiUIAPI
- 數獨遊戲九宮格遊戲
- C# 生成縮圖C#
- JS 專案拾粹 三: 唯一 ID 頭像圖片生成的 JS 庫JS
- Laravel-下載微信頭像到本地的介面Laravel
- Android 仿釘釘、微信 群聊組合頭像Android
- 【Javascript + Vue】實現隨機生成迷宮圖片JavaScriptVue隨機
- 教你基於MindSpore用DCGAN生成漫畫頭像
- 頭像
- Swift 4 - 模仿今日頭條Swift
- html5呼叫攝像頭截圖HTML
- laravel圖片/頭像上傳通用方法Laravel
- MacBook如何開啟微信攝像頭的許可權?Mac
- 微信小程式 MinUI 元件庫系列之 avatar 頭像元件微信小程式UI元件
- 微信小程式獲取base64頭像上傳微信小程式
- C# 使用AForge呼叫筆記本攝像頭拍照C#筆記
- 一個純前端實現的頭像生成網站前端網站
- win10微信影片無法使用攝像頭怎麼辦 win10電腦微信打不開攝像頭解決方法Win10
- win10微信視訊無法使用攝像頭怎麼辦 win10電腦微信打不開攝像頭解決方法Win10