前言
隨著一年一度的國慶假期越來越近,身邊的國慶氛圍也越來越重,很多人也開始換上了漸變國旗頭像,提前為祖國母親慶生。那每年都很火的漸變國旗頭像要如何制作呢?其實(shí)一點(diǎn)也不難!接下來就分享一種漸變國旗頭像
生成方法。
制作原理
上傳原始微信或其他頭像,將頭像的Image
對象用Graphics
創(chuàng)建返回GDI+
對象,然后用GDI+
對象在原始頭像指定位置進(jìn)行追加繪制指定大小的圖像渲染顯示。
項(xiàng)目架構(gòu)設(shè)計(jì)
演示項(xiàng)目為Winform窗體應(yīng)用程序,項(xiàng)目具體信息如下:
項(xiàng)目框架:
.NET Framework 4.8
項(xiàng)目架構(gòu)和窗體設(shè)計(jì):
五星紅旗模板準(zhǔn)備:
代碼實(shí)現(xiàn)
選擇頭像代碼:
/// <summary>
/// 選擇頭像
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_select_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);//初始路徑為桌面
openFileDialog.Filter = "頭像圖片|*.png;*.jpg";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
pic_old.Image = Image.FromFile(openFileDialog.FileName);
}
}
生成和切換模板代碼
/// <summary>
/// 生成或切換模板事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_change_Click(object sender, EventArgs e)
{
GenerateOrChangeTemplate();
}
/// <summary>
/// 生成頭像或切換模板生成頭像
/// </summary>
private void GenerateOrChangeTemplate()
{
try
{
if (templateFileInfos.Length == 0)
{
MessageBox.Show("紅旗模板集為空,請?zhí)砑?, "提示", MessageBoxButtons.OK);
return;
}
if (index >= templateFileInfos.Length)
{
index = 0;
}
Image head = (Image)pic_old.Image.Clone();
Image template = Image.FromFile(templateFileInfos[index].FullName);
Graphics graphics = Graphics.FromImage(head);
if (templateFileInfos[index].Name.StartsWith("all_"))
{
graphics.DrawImage(template, 0, 0, head.Width, head.Height);
}
else if (templateFileInfos[index].Name.StartsWith("right_"))
{
int x = head.Width / 4 * 3;
int y = head.Height / 4 * 3;
int w = head.Width / 4;
int h = head.Height / 4;
graphics.DrawImage(template, x, y, w, h);
}
else if (templateFileInfos[index].Name.StartsWith("left_"))
{
int y = head.Height - template.Height;
if (y < 0) y = 0;
graphics.DrawImage(template, 0, y, head.Width, head.Height);
}
pic_new.Image = head;
index++;
}
catch (Exception ex)
{
MessageBox.Show("出錯(cuò)了:"+ ex.Message,"警號(hào)",MessageBoxButtons.OK);
}
}
保存代碼
/// <summary>
/// 保存重新繪制的圖片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_save_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "圖片文件|*.png";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
pic_new.Image.Save(saveFileDialog.FileName, ImageFormat.Png);
MessageBox.Show("保存成功");
}
}
效果演示
源碼工具獲取
關(guān)注公眾號(hào),后臺(tái)回復(fù)關(guān)鍵字:五星紅旗頭像
文章來源:http://www.zghlxwxcb.cn/news/detail-711814.html
友情提示:僅供學(xué)習(xí)研究使用,切勿非法使用!文章來源地址http://www.zghlxwxcb.cn/news/detail-711814.html
到了這里,關(guān)于國慶期間“頭像+國旗”玩法是如何實(shí)現(xiàn)的?的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!