<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>幻想曲.Net &#187; GDI</title>
	<atom:link href="http://yibin.us/tag/gdi/feed" rel="self" type="application/rss+xml" />
	<link>http://yibin.us</link>
	<description>面朝大海，春暖花开</description>
	<lastBuildDate>Fri, 17 Jun 2011 01:57:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>丢弃Image类的GetThumbnailImage方法</title>
		<link>http://yibin.us/archives/5606</link>
		<comments>http://yibin.us/archives/5606#comments</comments>
		<pubDate>Fri, 17 Nov 2006 10:06:00 +0000</pubDate>
		<dc:creator>幻想曲</dc:creator>
				<category><![CDATA[Develop]]></category>
		<category><![CDATA[GDI]]></category>

		<guid isPermaLink="false">http://yibin.us/?p=5606</guid>
		<description><![CDATA[GetThumbnailImage的确是构造一个缩略图最简单的方法但同样存在一些不足:1、如当源图尺寸过大时,生成的缩略图质量会很低，而且同源图的尺寸是一种正比的关系2、当源图是一个Gif图片且含有透明色时，生成的缩略图会将透明色填充成黑色。 今天在工作中就遇到这样的问题，基于上面二个原因，于是决定放弃GetThumbnailImage方法了。最终采用Graphics类的DrawImage方法至于质量问题，适当的设定一下，可以达到很好的效果。 /// &#60;/summary&#62; /// &#60;param name="SourceFile"&#62;文件在服务器上的物理地址&#60;/param&#62; /// &#60;param name="strSavePathFile"&#62;保存在服务器上的路径&#60;/param&#62; /// &#60;param name="ThumbWidth"&#62;宽度&#60;/param&#62; /// &#60;param name="ThumbHeight"&#62;高度&#60;/param&#62; /// &#60;param name="BgColor"&#62;背景&#60;/param&#62; public static void myGetThumbnailImage(string SourceFile, string strSavePathFile, int ThumbWidth, int ThumbHeight, string BgColor) { System.Drawing.Image oImg = System.Drawing.Image.FromFile(SourceFile); //小图 int intwidth, intheight; if (oImg.Width &#62; oImg.Height) { if (oImg.Width &#62; ThumbWidth) { intwidth = ThumbWidth; intheight [...]]]></description>
			<content:encoded><![CDATA[<p>GetThumbnailImage的确是构造一个缩略图最简单的方法但同样存在一些不足:<br />1、如当源图尺寸过大时,生成的缩略图质量会很低，而且同源图的尺寸是一种正比的关系<br />2、当源图是一个Gif图片且含有透明色时，生成的缩略图会将透明色填充成黑色。</p>
<p>今天在工作中就遇到这样的问题，基于上面二个原因，于是决定放弃GetThumbnailImage方法了。<br />最终采用Graphics类的DrawImage方法至于质量问题，适当的设定一下，可以达到很好的效果。<br /><code><br />
/// &lt;/summary&gt;<br />
   /// &lt;param name="SourceFile"&gt;文件在服务器上的物理地址&lt;/param&gt;<br />
   /// &lt;param name="strSavePathFile"&gt;保存在服务器上的路径&lt;/param&gt;<br />
   /// &lt;param name="ThumbWidth"&gt;宽度&lt;/param&gt;<br />
   /// &lt;param name="ThumbHeight"&gt;高度&lt;/param&gt;<br />
   /// &lt;param name="BgColor"&gt;背景&lt;/param&gt;<br />
   public static void myGetThumbnailImage(string SourceFile, string strSavePathFile, int ThumbWidth, int ThumbHeight, string BgColor)<br />
   {<br />
       System.Drawing.Image oImg = System.Drawing.Image.FromFile(SourceFile);<br />
       //小图<br />
       int intwidth, intheight;<br />
       if (oImg.Width &gt; oImg.Height)<br />
       {<br />
           if (oImg.Width &gt; ThumbWidth)<br />
           {<br />
               intwidth = ThumbWidth;<br />
               intheight = (oImg.Height * ThumbWidth) / oImg.Width;<br />
           }<br />
           else<br />
           {<br />
               intwidth = oImg.Width;<br />
               intheight = oImg.Height;<br />
           }<br />
       }<br />
       else<br />
       {<br />
           if (oImg.Height &gt; ThumbHeight)<br />
           {<br />
               intwidth = (oImg.Width * ThumbHeight) / oImg.Height; intheight = ThumbHeight;<br />
           }<br />
           else<br />
           {<br />
               intwidth = oImg.Width; intheight = oImg.Height;<br />
           }<br />
       }<br />
       //构造一个指定宽高的Bitmap<br />
       Bitmap bitmay = new Bitmap(intwidth, intheight);<br />
       Graphics g = Graphics.FromImage(bitmay);<br />
       Color myColor;<br />
       if (BgColor == null) myColor = Color.FromName("white");<br />
       else<br />
           myColor = Color.FromName(BgColor);<br />
       //用指定的颜色填充Bitmap<br />
       g.Clear(myColor);<br />
       g.InterpolationMode = InterpolationMode.HighQualityBicubic;<br />
       //开始画图<br />
       g.DrawImage(oImg, new Rectangle(0, 0, intwidth, intheight), new Rectangle(0, 0, oImg.Width, oImg.Height), GraphicsUnit.Pixel);<br />
       bitmay.Save(strSavePathFile, System.Drawing.Imaging.ImageFormat.Jpeg);<br />
       g.Dispose();<br />
       bitmay.Dispose();<br />
       oImg.Dispose();<br />
       //删除源图<br />
       try<br />
       {<br />
           File.Delete(SourceFile);<br />
       }<br />
       catch { }<br />
   }
</pre>
]]></content:encoded>
			<wfw:commentRss>http://yibin.us/archives/5606/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>GDI+中对图片的裁剪已解决</title>
		<link>http://yibin.us/archives/5593</link>
		<comments>http://yibin.us/archives/5593#comments</comments>
		<pubDate>Tue, 07 Nov 2006 09:42:00 +0000</pubDate>
		<dc:creator>幻想曲</dc:creator>
				<category><![CDATA[Develop]]></category>
		<category><![CDATA[GDI]]></category>

		<guid isPermaLink="false">http://yibin.us/?p=5593</guid>
		<description><![CDATA[没想到Bitmap中的Clone方法可以解决这一切 void CutImage(HttpPostedFile post,string ppuid,out string imagename) { System.Drawing.Image SourceImg = System.Drawing.Image.FromStream(post.InputStream); if (SourceImg.Height &#62; ConfigHelper.UserFaceMaxHeight) { this._lbl_upload_msg.Text = "最大高度不得大于 " + ConfigHelper.UserFaceMaxHeight; return; } if (SourceImg.Width &#62; ConfigHelper.UserFaceMaxWidth) { this._lbl_upload_msg.Text = "最大宽度不得大于 " + ConfigHelper.UserFaceMaxWidth; return; } ImageFormat format = getImageformat(System.IO.Path.GetExtension(post.FileName)); string filename = ppuid+"."+format.ToString(); imagename = filename; if (!UserFaceDir.EndsWith("\\")) UserFaceDir = UserFaceDir+"\\"; filename [...]]]></description>
			<content:encoded><![CDATA[<p>没想到Bitmap中的Clone方法可以解决这一切<br /><code><br />
void CutImage(HttpPostedFile post,string ppuid,out string imagename)<br />
    {<br />
        System.Drawing.Image SourceImg = System.Drawing.Image.FromStream(post.InputStream);<br />
        if (SourceImg.Height &gt; ConfigHelper.UserFaceMaxHeight)<br />
        {<br />
            this._lbl_upload_msg.Text = "最大高度不得大于 " + ConfigHelper.UserFaceMaxHeight;<br />
            return;<br />
        }<br />
        if (SourceImg.Width &gt; ConfigHelper.UserFaceMaxWidth)<br />
        {<br />
            this._lbl_upload_msg.Text = "最大宽度不得大于 " + ConfigHelper.UserFaceMaxWidth;<br />
            return;<br />
        }<br />
        ImageFormat format = getImageformat(System.IO.Path.GetExtension(post.FileName));<br />
        string filename =  ppuid+"."+format.ToString();<br />
        imagename = filename;</p>
<p>        if (!UserFaceDir.EndsWith("\\"))<br />
            UserFaceDir = UserFaceDir+"\\";<br />
        filename = UserFaceDir + filename;<br />
        int SourceImgWidth = SourceImg.Width;<br />
        int SourceImgHeight = SourceImg.Height;<br />
        if ((SourceImgWidth != ConfigHelper.UserFaceWidth) &#038;&#038; (SourceImgHeight != ConfigHelper.UserFaceHeight))<br />
        {<br />
            //如果宽高比例为1:1,则直接构成缩略图<br />
            if (((Double)SourceImgWidth / SourceImgHeight) == 1)<br />
            {<br />
                System.Drawing.Image thumbimg = SourceImg.GetThumbnailImage(ConfigHelper.UserFaceWidth, ConfigHelper.UserFaceHeight, null, IntPtr.Zero);<br />
                thumbimg.Save(filename, format);<br />
                thumbimg.Dispose();<br />
                SourceImg.Dispose();<br />
                return;<br />
            }<br />
            Bitmap bit = new Bitmap(SourceImg);<br />
            Rectangle rec = new Rectangle();   //构造一个Rectangle类,一个矩形<br />
            rec.Width = ConfigHelper.UserFaceWidth;<br />
            rec.Height = ConfigHelper.UserFaceHeight;<br />
            if (SourceImgWidth &gt; rec.Width)<br />
                rec.X = (SourceImgWidth - rec.Width) / 2;<br />
            else<br />
            {<br />
                rec.X = 0;<br />
                rec.Width = SourceImg.Width;<br />
            }<br />
            if (SourceImgHeight &gt; rec.Height)<br />
                rec.Y = (SourceImgHeight - rec.Height) / 2;<br />
            else<br />
            {<br />
                rec.Y = 0;<br />
                rec.Height = SourceImg.Height;<br />
            }</p>
<p>            try<br />
            {<br />
                //这里就是把从上传过程中构造的bitmap克隆一份,并按定义好的矩形裁剪<br />
                bit.Clone(rec, PixelFormat.DontCare).Save(filename, format);<br />
            }<br />
            catch (Exception ex)<br />
            {<br />
                this._lbl_upload_msg.Text = ex.Message;<br />
                return;<br />
            }<br />
            finally<br />
            {<br />
                bit.Dispose();<br />
                SourceImg.Dispose();<br />
            }<br />
        }<br />
    }
</pre>
<p>写得很粗糙,暂时不支持多帧的gif图片，也没做生成图片时质量方面的考虑，明天解决这一切。</p>
]]></content:encoded>
			<wfw:commentRss>http://yibin.us/archives/5593/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>对Graphics.DrawImageUnscaledAndClipped的疑虑</title>
		<link>http://yibin.us/archives/5592</link>
		<comments>http://yibin.us/archives/5592#comments</comments>
		<pubDate>Tue, 07 Nov 2006 06:01:00 +0000</pubDate>
		<dc:creator>幻想曲</dc:creator>
				<category><![CDATA[Develop]]></category>
		<category><![CDATA[GDI]]></category>

		<guid isPermaLink="false">http://yibin.us/?p=5592</guid>
		<description><![CDATA[一直没有深入了解研究c#中的GDI+,仅仅只是在应用中构造缩略图或加一下水印在今天的项目中，遇到以下的需求用户在上传图片后，统一调整为64*64；宽高大于64的，从图片中间截取，先不论这种设计合不合理，现在问题出现在对原始图片的裁剪上。.Net中提供了Image类与Graphics类，这二个类可以达到目的。 System.Drawing.Image SourceImg = System.Drawing.Image.FromStream(this.FileUpload1.PostedFile.InputStream); int SourceImgWidth = SourceImg.Width; //图片的原始Width int SourceImgHeight = SourceImg.Height; //图片的原始Height if ((SourceImgWidth != 64) &#38;&#38; (SourceImgHeight != 64)) { //System.Drawing.Imaging.ImageFormat f = g.RawFormat; Bitmap b = new Bitmap(64, 64); Graphics gh = Graphics.FromImage(b); gh.DrawImageUnscaledAndClipped(SourceImg, new Rectangle(0, 0, SourceImgWidth, SourceImgHeight)); gh.SmoothingMode = SmoothingMode.AntiAlias; b.Save(@&#34;C:\yy1.jpg&#34;); gh.Dispose(); b.Dispose(); SourceImg.Dispose(); } 查SDK中对于DrawImageUnscaledAndClipped方法的解释：在不进行缩放的情况下绘制指定的图像，并在需要时剪辑该图像以适合指定的矩形。 我的理解是：gh.DrawImageUnscaledAndClipped(SourceImg, new [...]]]></description>
			<content:encoded><![CDATA[<p>一直没有深入了解研究c#中的GDI+,仅仅只是在应用中构造缩略图或加一下水印<br />在今天的项目中，遇到以下的需求<br />用户在上传图片后，统一调整为64*64；宽高大于64的，从图片中间截取，先不论这种设计合不合理，现在问题出现在对原始图片的裁剪上。<br /><a href="http://yibin.us/zh-cn/uploads/061107-125940_snap00021.gif"  class="thickbox"><img src="http://yibin.us/zh-cn/uploads/061107-125940_snap00021.gif" class="img"  /></a><br />.Net中提供了Image类与Graphics类，这二个类可以达到目的。<br /><code><br />
 System.Drawing.Image SourceImg = System.Drawing.Image.FromStream(this.FileUpload1.PostedFile.InputStream);<br />
            int SourceImgWidth = SourceImg.Width;  //图片的原始Width<br />
            int SourceImgHeight = SourceImg.Height;  //图片的原始Height<br />
            if ((SourceImgWidth != 64) &amp;&amp; (SourceImgHeight != 64))<br />
            {<br />
                //System.Drawing.Imaging.ImageFormat f = g.RawFormat;<br />
                Bitmap b = new Bitmap(64, 64);<br />
                Graphics gh = Graphics.FromImage(b);<br />
                gh.DrawImageUnscaledAndClipped(SourceImg, new Rectangle(0, 0, SourceImgWidth, SourceImgHeight));<br />
                gh.SmoothingMode = SmoothingMode.AntiAlias;<br />
                b.Save(@&quot;C:\yy1.jpg&quot;);<br />
                gh.Dispose();<br />
                b.Dispose();<br />
                SourceImg.Dispose();<br />
            }
</pre>
<p>查SDK中对于DrawImageUnscaledAndClipped方法的解释：<br /><b>在不进行缩放的情况下绘制指定的图像，并在需要时剪辑该图像以适合指定的矩形。 </b></p>
<p>我的理解是：<br />gh.DrawImageUnscaledAndClipped(SourceImg, new Rectangle(0, 0, 64, 64));<br />在源图上剪辑一指定矩形的区域，在Rectangel结构中已经定义了x坐标为0,y坐标为0,<br />执行上面的代码，生成的图片为<br /><a href="http://yibin.us/zh-cn/uploads/061107-125948_yy1.jpg"  class="thickbox"><img src="http://yibin.us/zh-cn/uploads/061107-125948_yy1.jpg" class="img"  /></a><br />将Retangle中的x坐标稍做修改,为了说明问题，将空白部分用黑色填充了<br />gh.DrawImageUnscaledAndClipped(SourceImg, new Rectangle(10, 0, 64, 64));<br />生成的图片<br /><a href="http://yibin.us/zh-cn/uploads/061107-131033_yy1.jpg"  class="thickbox"><img src="http://yibin.us/zh-cn/uploads/061107-131033_yy1.jpg" class="img"  /></a><br />看来Rectangle中的x坐标并不是决定在源图中要裁剪的x坐标值<br />那到底如何在对一张源图进行裁剪呢?  </p>
]]></content:encoded>
			<wfw:commentRss>http://yibin.us/archives/5592/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

