Magick.NET库测试

cs 复制代码
        private void button4_Click(object sender, EventArgs e)
        {
            uint width = 100;
            uint height = 100;
            int[,] pixels = new int[height, width];
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    // 示例:生成简单的渐变效果
                    byte red = (byte)(x * 255 / width);
                    byte green = (byte)(y * 255 / height);
                    byte blue = (byte)((x + y) * 255 / (width + height));
                    pixels[y, x] = (red << 16) | (green << 8) | blue; // RGB格式存储为整数
                }
            }
            using (var image = new MagickImage(MagickColor.FromRgba(0, 0, 0, 255), width, height))
            {
                for (var y = 0; y < height; y++)
                {
                    for (var x = 0; x < width; x++)
                    {
                        int pixelValue = pixels[y, x];
                        byte r = (byte)((pixelValue >> 16) & 0xFF);
                        byte g = (byte)((pixelValue >> 8) & 0xFF);
                        byte b = (byte)((pixelValue >> 0) & 0xFF);
                        Pixel p = new Pixel(x, y, new byte[] { r, g, b });
                        image.GetPixels().SetPixel(p);
                    }
                }
                image.Settings.Font = this.Font.Name;
                image.Settings.FillColor = MagickColors.Red;
                image.Settings.StrokeColor = MagickColors.Red;
                image.Annotate("test",Gravity.Center);
                image.Write("D:\\1.bmp");
                image.Format = MagickFormat.Bmp;
                using (var stream = new MemoryStream())
                {
                    image.Write(stream);
                    stream.Position = 0;
                    var bitmap = new Bitmap(stream);
                    pictureBox1.Image?.Dispose();
                    pictureBox1.Image = bitmap;
                }
            }
        }

生成图像大小可40000x40000,对大size图像支持较好,并实现了高斯模糊、Resize、裁剪、压缩、旋转等常见操作。

相关推荐
唐青枫1 天前
C#.NET Monitor 与 Mutex 深入解析:进程内同步、跨进程互斥与使用边界
c#·.net
会写代码的建筑师1 天前
.NET 控制台后台程序实践细节总结
后端·.net
阿捞21 天前
在 .NET 中使用 Moonshot Kimi + AgentFramework:从 SDK 到 Agent 的完整实践
html·.net·xhtml
步步为营DotNet1 天前
解锁.NET 11 中 Microsoft.Extensions.AI 在智能后端开发的深度应用
人工智能·microsoft·.net
无风听海1 天前
.NET10之C# 中的is null深入理解
服务器·c#·.net
bjzhang751 天前
FastReport——一个面向.NET生态的开源报表引擎
.net·fastreport
武藤一雄2 天前
C# 异常(Exception)处理避坑指南
windows·microsoft·c#·.net·.netcore·鲁棒性
步步为营DotNet2 天前
剖析.NET 11 中 Native AOT 在高性能客户端应用的极致实践
.net
步步为营DotNet2 天前
深度探索.NET Aspire在云原生应用性能与安全加固的创新实践
安全·云原生·.net
我是唐青枫2 天前
C#.NET TPL Dataflow 深入解析:数据流管道、背压控制与实战取舍
c#·.net