C#使用Graphics绘图通过加载纹理底图的方式绘制纹理效果

纹理图片

加载效果如下图:

在扇形上把纹理图片作为底,渲染到绘制的图像中。

图形渲染中,纹理可以包括各种图像,如照片、图标、图案等。这些图像被映射到三维模型的表面,使得表面看起来像是被这些图像所包裹。

主要特点和用途包括:

  • 表面细节: 纹理能够为三维模型提供更为细致的表面细节,使其看起来更加真实,避免了简单的单一颜色或光照。

  • 模拟材质: 通过合理的纹理映射,可以模拟出各种材质,如木纹、石头、金属等,使得渲染结果更加逼真。

  • 增强视觉效果: 使用纹理可以增强图形渲染的视觉效果,如镜面反射、折射、法线映射等。

代码如下:

xml 复制代码
     private void OnPaint1(object sender, PaintEventArgs e)
     {
         // 创建一个Graphics对象
         Graphics g = e.Graphics;


         int width = 500; // 圆的宽度

         g.DrawArc(new Pen(Color.Black, 5), width / 2, width / 2, 500, 500, 0f, -90);

         // 定义弧形的矩形区域
         Rectangle rect = new Rectangle(50, 50, 500, 500);

         // 定义弧形的起始角度和扫描角度(以度为单位)
         float startAngle = 45.0f;
         float sweepAngle = 90.0f;

         // 创建一个纹理图像(这里使用一个示例图像)
         Image textureImage = Image.FromFile("texture.png");

         // 创建一个Bitmap对象来处理透明度
         Bitmap transparentBitmap = new Bitmap(rect.Width, rect.Height);

         // 在这个示例中,将透明度设置为50%,可以根据需要调整
         int alpha = 100; // 0 (透明) 到 255 (不透明) 之间的值
         ColorMatrix colorMatrix = new ColorMatrix(new float[][]{
             new float[] {1, 0, 0, 0, 0},
             new float[] {0, 1, 0, 0, 0},
             new float[] {0, 0, 1, 0, 0},
             new float[] {0, 0, 0, alpha / 255f, 0},
             new float[] {0, 0, 0, 0, 1}
         });

         using (ImageAttributes imageAttributes = new ImageAttributes())
         {
             imageAttributes.SetColorMatrix(colorMatrix);

             // 使用ImageAttributes设置透明度
             using (Graphics transparentGraphics = Graphics.FromImage(transparentBitmap))
             {
                 transparentGraphics.DrawImage(textureImage, new Rectangle(0, 0, transparentBitmap.Width, transparentBitmap.Height),
                     0, 0, textureImage.Width, textureImage.Height, GraphicsUnit.Pixel, imageAttributes);
             }
         }

         // 创建一个TextureBrush,并将纹理图像关联到它
         TextureBrush textureBrush = new TextureBrush(transparentBitmap);

         // 绘制扇形并使用纹理填充
         g.SmoothingMode = SmoothingMode.AntiAlias; // 使绘制更平滑
         g.FillPie(Brushes.Yellow, rect, startAngle, sweepAngle);

         g.FillPie(textureBrush, rect, startAngle, sweepAngle);

         // 释放纹理图像资源
         textureImage.Dispose();

     }
相关推荐
珹洺1 小时前
JSP技术入门指南【一】利用IDEA从零开始搭建你的第一个JSP系统
java·开发语言·前端·html·intellij-idea·jsp
·醉挽清风·2 小时前
学习笔记—C++—模板初阶
开发语言·c++·笔记·学习
User_芊芊君子2 小时前
跨平台开发选Java还是C?应用场景与性能深度对比
java·c语言·开发语言
一只小松许️3 小时前
Rust泛型与特性
java·开发语言·rust
搬砖工程师Cola5 小时前
<C#>在 C# .NET 6 中,使用IWebHostEnvironment获取Web应用程序的运行信息。
开发语言·c#·.net
八了个戒7 小时前
「数据可视化 D3系列」入门第三章:深入理解 Update-Enter-Exit 模式
开发语言·前端·javascript·数据可视化
失去妙妙屋的米奇7 小时前
matplotlib数据展示
开发语言·图像处理·python·计算机视觉·matplotlib
夏天的阳光吖7 小时前
C++蓝桥杯实训篇(四)
开发语言·c++·蓝桥杯
angushine8 小时前
Gateway获取下游最终响应码
java·开发语言·gateway
西贝爱学习8 小时前
数据结构:C语言版严蔚敏和解析介绍,附pdf
c语言·开发语言·数据结构