当前位置: 首页 > news >正文

ASP.NET Core SixLabors.ImageSharp 位图图像创建和下载

        从 MVC 控制器内部创建位图图像并将其发送到浏览器;用 C# 编写并与 Linux 和 Windows 服务器兼容。

使用从 ASP.NET MVC 中的控制器下载任何文件类型File。

此示例创建一个位图 (jpeg) 并将其发送到浏览器。它需要 NuGet 包SixLabors.ImageSharp v1.0.4。

另请参阅:SixLabors.ImageSharp 图像实用程序类

另请参阅:将文件上传到浏览器后删除

下面的代码将创建以下四幅图像并在它们之间交替显示:

修改后的HomeController.cs文件: 

// HomeController.cs
using Microsoft.AspNetCore.Mvc;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;

namespace WebApplication1.Controllers
{
    public class HomeController : Controller
    {
        static private byte mod(int x, int y) // NOTE: only returns values 0 to 255
        {
            if ((y & 1) == 0) // NOTE: then base of 2 - no need for division
                return (byte)(x & (y - 1));
            else
                return (byte)(x % y);
        }
        static private void Image0(Image<Rgb24> bmp) // NOTE: this will create a colorful bitmap with colorful stars
        {
            // NOTE: loop through every pixel of the bitmap
            for (int x = 0; x < bmp.Width; x++)
                for (int y = 0; y < bmp.Height; y++)
                {
                    byte p = (byte)(x * y >> 8), c = mod(p, 3), z = 255; // NOTE: "z" can be any shade between 0 and 255
                    bmp[x, y] = new Rgb24(c == 0 ? p : z, c == 1 ? p : z, c == 2 ? p : z); // NOTE: set the color of the pixel at (x, y)
                }
        }
        static private void Image1(Image<Rgb24> bmp) // NOTE: this will create a bitmap with a pattern that repeats
        {
            // NOTE: loop through every pixel of the bitmap
            for (int x = 0; x < bmp.Width; x++)
                for (int y = 0; y < bmp.Height; y++)
                {
                    byte p = mod(x * y, 256), c = mod(p, 3), z = 0; // NOTE: "z" can be any shade between 0 and 255
                    bmp[x, y] = new Rgb24(c == 0 ? p : z, c == 1 ? p : z, c == 2 ? p : z); // NOTE: set the color of the pixel at (x, y)
                }
        }
        static private void Image2(Image<Rgb24> bmp) // NOTE: this will create a bitmap with a unique diagonal pattern
        {
            // NOTE: loop through every pixel of the bitmap
            for (int x = 0; x < bmp.Width; x++)
                for (int y = 0; y < bmp.Height; y++)
                {
                    byte p = mod(x, y), c = mod(p, 3), z = 127; // NOTE: "z" can be any shade between 0 and 255
                    bmp[x, y] = new Rgb24(c == 0 ? p : z, c == 1 ? p : z, c == 2 ? p : z); // NOTE: set the color of the pixel at (x, y)
                }
        }
        static private void Image3(Image<Rgb24> bmp) // NOTE: this will create a bitmap of noise
        {
            var rand = new Random();
            // NOTE: loop through every pixel of the bitmap
            for (int x = 0; x < bmp.Width; x++)
                for (int y = 0; y < bmp.Height; y++)
                {
                    byte p = mod(rand.Next(), 256), c = mod(p, 3), z = 255; // NOTE: "z" can be any shade between 0 and 255
                    bmp[x, y] = new Rgb24(c == 0 ? p : z, c == 1 ? p : z, c == 2 ? p : z); // NOTE: set the color of the pixel at (x, y)
                }
        }
        public IActionResult Index()
        {
            int image = int.Parse(Request.Cookies["image"] ?? "0");
            Response.Cookies.Append("image", (image + 1).ToString());
            const int shift = 9;
            const int width = 1 << shift, height = 1 << shift;
            using (var bmp = new Image<Rgb24>(width, height)) // NOTE: create 24-bit bitmap
            {
                switch (mod(image, 4))
                {
                    case 0:
                        Image0(bmp);
                        break;
                    case 1:
                        Image1(bmp);
                        break;
                    case 2:
                        Image2(bmp);
                        break;
                    case 3:
                        Image3(bmp);
                        break;
                }
                byte[] data;
                using (var ms = new MemoryStream())
                {
                    bmp.SaveAsJpeg(ms); // NOTE: save the bitmap as a JPEG image to the stream
                    data = ms.ToArray();

                }
                // NOTE: alternatively, send the data to the browser as a download with the file name "attachment.jpg" by uncommenting the following line
                // return File(data, System.Net.Mime.MediaTypeNames.Image.Jpeg, "attachment.jpg");

                return File(data, System.Net.Mime.MediaTypeNames.Image.Jpeg); // NOTE: send the data to the browser as a file of mime type "image/jpeg"
            }
        }
    }
}

如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。

相关文章:

  • 2月14日情人节,致挚爱
  • Linux 驱动开发:字符设备、块设备与网络设备驱动详解​​
  • Linux驱动层学习:LED 驱动开发
  • 前端开发入门一
  • c# 对象属性拷贝 解决方案
  • NPDP学习笔记 -产品经理(第二版)-第二章 组合管理
  • 为什么vue3需要对引入的组件使用markRaw?
  • 【Elasticsearch】词项中心(term-centric)和字段中心(field-centric)
  • 10bit VS 8bit 视频:色彩深度的较量,谁才是视觉盛宴的王者?
  • 【Sceneform-EQR】实现3D场景背景颜色的定制化(背景融合的方式、Filament材质定制)
  • OpenLayer创建第一个基础地图实例
  • “集团企业浪潮”(Conglomerate Wave):市盈率套利(P/E Arbitrage)与每股盈利增长的幻象
  • 基于深度学习的半导体领域关键技术创新与应用突破
  • python调用多平台deepseek等大模型api
  • 求助文心一言帮我用antv x6开发一个直线审批流程设计页面Vue2.0
  • int* a = new int(3);delete a;后会调用析构函数吗?
  • ClickHouse的前世今生
  • Training for Computer Use
  • 【实战AI】利用deepseek 在mac本地部署搭建个人知识库
  • 堆栈欺骗技术
  • 哪些网站是jsp做的/企业网站网页设计
  • 过年做啥网站致富/厦门百度seo公司
  • 舟山的房子做民宿上什么网站/湖南网站定制
  • 黄一级a做爰片免费网站/成都排名seo公司
  • 私服网站建设/潍坊百度快速排名优化
  • 开发网站平台/企业网站seo案例