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、裁剪、压缩、旋转等常见操作。

相关推荐
深海潜水员3 小时前
【MonoGame游戏开发】| 牧场物语实现 第一卷 : 农场基础实现 (下)
vscode·游戏·c#·.net·monogame
时光追逐者16 小时前
Visual Studio 2026 现已正式发布,更快、更智能!
ide·c#·.net·visual studio
用户7227868123442 天前
.NET 实现雪花算法:高效生成分布式唯一 ID
.net
玩泥巴的2 天前
.NET 8+ 飞书API实战:自动化群组管理与消息推送
c#·.net·二次开发·飞书
唐青枫2 天前
C#.NET 范围与索引(Range、Index)完全解析:语法、用法与最佳实践
c#·.net
许泽宇的技术分享3 天前
当AI Agent遇上.NET:微软Agent Framework的架构奥秘与实战启示
人工智能·microsoft·.net
SEO-狼术3 天前
DevExpress DXperience Crack
.net
我是唐青枫3 天前
一文理解 C#.NET Tuples:从基础到高级应用
c#·.net
缺点内向3 天前
C# 中 Word 文档目录的插入与删除指南
开发语言·c#·word·.net
唐青枫3 天前
告别 if-else:C#.NET 模式匹配让代码更优雅的正确方式
c#·.net