生活随笔
收集整理的這篇文章主要介紹了
宝宝年龄动态图片
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
記得以前籬笆網(wǎng)以前是生成動態(tài)的生日提醒圖片,后來自己決定給自己寶寶做一個。但是一直 都沒空去弄,最近比較空所以做了,其實做的時候主要是PNG與圖片 (任何格式j(luò)pg,gif,bmp)的重疊與文字寫入最后生成圖片PNG!,用到了計算年寶寶生日年齡的計算細到幾歲幾月幾天下面會介紹方法 getAge!文章末尾附下載地址
效果圖如下:
相信大家看了這張合成圖后應(yīng)該了解是怎么做的了!對了就是兩張圖片合成的!
圖1:(畫布)
圖2:(我把它叫遮罩層)
當然圖片一定必須是PNG這樣才能讓寶寶的照片顯示出來!
好了貼代碼了!
前臺代碼:
C#代碼
<%@?Page?Language= "C#" ?AutoEventWireup= "true" ??CodeFile= "SAVEPNG.aspx.cs" ?Inherits= "_SAVEPNG" ?%>?? <!DOCTYPE?html?PUBLIC?"-//W3C//DTD?XHTML?1.0?Transitional//EN" ? "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >?? <html?xmlns="http://www.w3.org/1999/xhtml" >?? <head?runat="server" >?? ????<title>生成寶寶年齡圖</title>?? </head>?? <body>?? ????<form?id="form1" ?runat= "server" >?? ????<asp:Button?ID="Button1" ?runat= "server" ?Text= "生成寶寶年齡圖" ?οnclick= "Button1_Click" ?/>?? ????</form>?? </body>?? </html>?? 后臺代碼:
C#代碼
using ?System.Drawing;?? using ?System;?? using ?System.IO;?? public ?partial? class ?_Default?:?System.Web.UI.Page?? {?? ????protected ? void ?Page_Load( object ?sender,?EventArgs?e)?? ????{}?? ????protected ? void ?Button1_Click( object ?sender,?EventArgs?e)?? ????{?? ????????if ?(!Directory.Exists(Server.MapPath( "img" )))?? ????????????Directory.CreateDirectory(Server.MapPath("img" ));?? ???????? ?? ????????string ?path1?=?Server.MapPath( "img/test.png" );?? ?? ???????? ?? ????????System.Drawing.Image?image?=?System.Drawing.Image.FromFile(Server.MapPath("images.png" ));?? ?? ???????? ?? ????????System.Drawing.Image?syImage?=?System.Drawing.Image.FromFile(Server.MapPath("psb.png" ));?? ???????? ?? ????????Graphics?g?=?Graphics.FromImage(image);?? ???????? ?? ????????g.DrawImage(syImage,?new ?Rectangle(0,?0,?syImage.Width,?syImage.Height),?0,?0,?syImage.Width,?syImage.Height,?GraphicsUnit.Pixel);?? ?? ???????? ?? ????????string ?str?=?getAge( "2010-3-17" );?? ?? ????????Bitmap?images?=?new ?Bitmap(482,?250); ?? ???????? ?? ???????? ?? ???????? ?? ????????g.DrawString(str,?new ?Font( "宋體" ,?12),? new ?SolidBrush(Color.FromArgb(255,?0,?138)),?195,?53);?? ?? ???????? ?? ?? ????????g.Dispose();?? ???????? ?? ????????image.Save(path1,?System.Drawing.Imaging.ImageFormat.Png);?? ????}?????? ??? ? ????#region?計算年齡 ?? ???? ?? ???? ?? ???? ?? ???? ?? ???? ?? ????private ? string ?getAge( string ?bornDate)?? ????{?? ?? ????????DateTime?b?=?DateTime.Parse(bornDate);?? ????????int ?a2?=?DateTime.Now.Year;?? ????????int ?b2?=?b.Year;?? ????????int ?a3?=?DateTime.Now.Month;?? ????????int ?b3?=?b.Month;?? ????????int ?a4?=?DateTime.Now.Day;?? ????????int ?b4?=?b.Day;?? ????????int ?aa?=?(a2?-?1900)?*?365?+?a3?*?30?+?a4;?? ????????int ?bb?=?(b2?-?1900)?*?365?+?b3?*?30?+?b4;?? ????????int ?cc?=?aa?-?bb;? ?? ????????int ?dd?=?( int )Math.Floor(Convert.ToDouble(cc?/?365));?? ????????int ?days?=?cc?-?dd?*?365;?? ????????int ?ee?=?days?/?30;?? ????????int ?day?=?days?-?ee?*?30;?? ????????return ? "今天是" ?+?DateTime.Now.ToString( "D" )?+? "?寶寶已經(jīng)" ?+?dd.ToString()?+? "歲" ?+?ee.ToString()?+? "個月?零" ?+?day?+? "天啦" ;?? ????}?? ????#endregion ?? }?? 好了已經(jīng)寫完了,源碼分享下!下載地址
提醒:
如果遇到GDI+中發(fā)生一般性錯誤
方法1、img文件夾不要建立程序會自己建立的(我是這么實現(xiàn)的)
方法2、解決辦法如下:.net中 GDI+ 中發(fā)生一般性錯誤 解決辦法
如需轉(zhuǎn)載請注明出處:http://www.ok22.org/art_detail.aspx?id=406
【寶寶年齡動態(tài)圖片】幸凡學習網(wǎng)
總結(jié)
以上是生活随笔為你收集整理的宝宝年龄动态图片的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。