利用TextureBrush时的图像偏移问题。

xiaoxiao2021-03-01  22

最近使用GDI+中的TextureBrush ,但是在FillRectangle時,圖像總是出現偏移問題,例如下圖:

原始圖片應該為:

下面是我的源代碼,我將一步一步的分析出現這個問題的原因及解決方法。

Image logo = Image.FromFile( " Your Picture Path" );TextureBrush br = new TextureBrush( logo );Graphics g = this.CreateGraphics();g.FillRectangle( br , 22 , 29 , 20 , 20 );

原因就在於g.FillRectangle( br , 22 , 29 , 20 , 20 ); 中的22 ,29 。原來我認為22 ,29 隻是來確定你要顯示的圖片在form中的相對位置( x ,y ),但是用TextureBrush時(22 ,29) ,不僅是確定位置的,而且還決定了圖片的偏移大小 ,你如果把其換成其它的數,那麼圖片的偏移還會變的。把g.FillRectangle( br , 22 , 29 , 20 , 20 ); 改成g.FillRectangle( br ,0 ,0 , 20 , 20 ); ,圖像顯示正常,但是其位置確是(0, 0),所以這也驗証了我上述的猜想是正確的。

解決方法:

Image logo = Image.FromFile( "D:\\EPON_EMS\\image\\green.png" );TextureBrush br = new TextureBrush( logo );br.TranslateTransform( 22 , 29 );Graphics g = LIU1.CreateGraphics();g.FillRectangle( br , 22 , 29 , 20 , 20 );

加上一句br.TranslateTransform( 22 , 29 );就OK了,TranslateTransform是把原始圖像的起始位置進行偏移,這樣我們就相當於先將它偏移,然後在FillRectangle中又將它偏移了過來,這樣簡單的達到了我們的目的。

相关资源:微信小程序源码-合集1.rar
转载请注明原文地址: https://www.6miu.com/read-3450064.html

最新回复(0)