最近使用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中又將它偏移了過來,這樣簡單的達到了我們的目的。
分享到:
相关推荐
主要介绍了C#纹理画刷TextureBrush用法,实例分析了纹理画刷TextureBrush的具体使用技巧,需要的朋友可以参考下
• HatchBrush:类似于 SolidBrush,但是可以利用该类从大量预设的图案中选择绘制时要使用的图案,而不是纯色 • TextureBrush:使用纹理(如图像)进行绘制 • LinearGradientBrush:使用沿渐变混合的两种颜色...
TextureBrush MyBrush = new TextureBrush(MyBitmap); switch(this.iFlag) { case 1: MyBrush.RotateTransform(0.0f); //正常显示图像 break; case 2: MyBrush.RotateTransform...
用图像平铺形状程序(C#源代码编写)private void Form1_Paint(object sender, PaintEventArgs e) { Image image = new Bitmap("..\\..\\test.bmp"); TextureBrush tBrush = new TextureBrush(image); tBrush.Wrap...
摘要:C#源码,图形图像,图像旋转 任意角度的C#图像旋转程序源码,一过目前仅支持BMP图像,其它图像不知为何不能旋转.通过字符串实例化Bitmap类,实例化panel控件的Graphics类,使图片旋转360度,实例化TextureBrush...
TextureBrush b = new TextureBrush(new Bitmap(@"E:\壁纸\叶子.jpg")); Graphics g = this.CreateGraphics(); g.FillEllipse(b, 50, 50, 150, 150); } private void button4_Click(object sender, EventArgs...
7、解决了DataGridView在有固定列(冻结列)的时候,有些情况下(DGV固定列的index与DisplayIndex不一致时)打印可能出错的问题。 8、DGVPrint组件的自定义纸张使用更方便,设计时在可以通过设置PaperHeight与...
Image textImage = new ... TextureBrush textTextureBrush = new TextureBrush(textImage); Font myFont = new Font("SansSerif", 50); e.Graphics.DrawString("Hello gouyue", myFont, textTextureBrush, 10, 20);
TextureBrush tBrush = new TextureBrush(bitmap); Pen texturedPen = new Pen(tBrush, 30); e.Graphics.DrawImage(bitmap, 0, 0, bitmap.Width, bitmap.Height); e.Graphics.DrawEllipse(texturedPen, 100, 20...