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

相关推荐
Y3815326621 天前
C# 调用谷歌搜索 API:HttpClient 与 System.Text.Json 实战
搜索引擎·.net
qq_410194292 天前
.NET 8.0 下基于 PaddleOCRSharp 的离线文字识别落地指南
算法·.net
唐青枫3 天前
别急着拆服务:C#.NET 微服务架构从边界设计到实战
c#·.net
唐青枫4 天前
别把 Razor 当成几段 HTML:C#.NET Razor Pages、MVC 视图与实战详解
c#·.net
宝桥南山4 天前
Microsoft Fabric - 简单尝试一下Microsoft Fabric .NET SDK
microsoft·微软·.net·.netcore·powerbi·fabric
Lost of 程序猿5 天前
.NET 10 船端单机部署与离线升级实战:从闪退事故到无感回滚
c#·asp.net·.net
慧都小妮子5 天前
Word/Excel/PPT 如何稳定导出 Markdown?文档 SDK 三线能力拆解
.net·markdown·知识库·aspose·rag·文档转换·文档互操作
唐青枫5 天前
对象到底住在哪里?C#.NET 托管堆从分配、GC 到内存排查
c#·.net
geovindu6 天前
CSharp:Condition Variable Pattern
后端·设计模式·c#·.net·.netcore·条件变量模式·同步型模式
de之梦-御风6 天前
【未来】.NET 开发在「劳动价值重估」下的定位与走向
职场和发展·.net