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

相关推荐
天马379811 小时前
.net 8.0 System.InvalidOperationException:“Session has not been configured
.net
-可乐加冰吗19 小时前
SuperMap iObjects .NET 11i 二次开发(十七)—— 叠加分析之擦除
windows·.net
一抓掉一大把20 小时前
RuoYi .net-实现商城秒杀下单(redis,rabbitmq)
redis·mysql·c#·rabbitmq·.net
缺点内向1 天前
C#: 高效移动与删除Excel工作表
开发语言·c#·.net·excel
一抓掉一大把2 天前
.net实现秒杀商品(Redis高并发)
.net
mudtools2 天前
解放双手!使用Roslyn生成代码让你的 HTTP 客户端开发变得如此简单
低代码·c#·.net
主宰者2 天前
WPF CalcBinding简化判断逻辑
c#·.net·wpf
【D'accumulation】2 天前
.NET Framework 4.8 + Microsoft.Data.Sqlite 报 Library e_sqlite3 not found
microsoft·sqlite·.net
数据的世界012 天前
技术变革:为何C#与.NET是未来的开发方向
java·c#·.net