NetCore3.1生成模板图片

NetCore3.1生成模板图片

在C#中要对图片操作就要用到Graphics的类
BLL:

public MemoryStream Img(string name,string amount,string date)
        {
            Bitmap bitmap = new Bitmap(GetPdfTemplatePath());
            Graphics g = Graphics.FromImage(bitmap);
            SolidBrush solidBrush = new SolidBrush(Color.Black);
            g.DrawString(name, new System.Drawing.Font("宋体", 14), solidBrush, 120, 250);
            g.DrawString(amount, new System.Drawing.Font("宋体", 14), solidBrush,600,320);
            g.DrawString(date, new System.Drawing.Font("宋体", 14), solidBrush, 680, 505);
            MemoryStream ms = new MemoryStream();
            bitmap.Save(ms, ImageFormat.Jpeg);
            byte[] newByte = ms.ToArray();
            return ms;
        }

Controller:

  public FileContentResult DownloadImg()
        {
            var name = Request.Params("Name");
            var amount = Request.Params("Amount");
            var createtime = Request.Params("CreateTime");

            CertificateHelper certificateHelper = new CertificateHelper();
            MemoryStream ms = certificateHelper.Img(name,amount,createtime);
            return File(ms.ToArray(), @"image/jpeg");
        }

前台接收:从文件流生成文件下载

在Ajax的请求中,要设置responseType: ‘blob’

  downloadimg (params) {
      let self = this
      const data = {
        Name: params.row.Name,
        Amount: params.row.Amount,
        CreateTime: self.formatTime(params.row.CreateTime * 1000)
      }
      api.downloadimg(data).then(function (res) {
        self.download(res)
      })
    },
    download (data) {
      let self = this
      if (!data) {
        return
      }
      let url = window.URL.createObjectURL(new Blob([data]))
      let link = document.createElement('a')
      self.imgsrc = url
      link.style.display = 'none'
      link.href = url
      link.setAttribute('download', '荣誉证书.jpeg')
      document.body.appendChild(link)
      link.click()
    },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
.NET Core 3.1 中,你可以使用内置的 `System.Net.WebSockets` 命名空间来实现 WebSocket 通信。以下是使用 .NET Core 3.1 实现 WebSocket 服务器和客户端的基本步骤: ## 实现 WebSocket 服务器 1. 创建一个 .NET Core Web 应用程序。 2. 在 `Startup.cs` 文件中,添加 WebSocket 中间件: ```csharp public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { // ... app.UseWebSockets(); app.Use(async (context, next) => { if (context.Request.Path == "/ws") { if (context.WebSockets.IsWebSocketRequest) { WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync(); await Echo(webSocket); } else { context.Response.StatusCode = 400; } } else { await next(); } }); // ... } private async Task Echo(WebSocket webSocket) { byte[] buffer = new byte[1024]; WebSocketReceiveResult result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None); while (!result.CloseStatus.HasValue) { await webSocket.SendAsync(new ArraySegment<byte>(buffer, 0, result.Count), result.MessageType, result.EndOfMessage, CancellationToken.None); result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None); } await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None); } ``` 上面的代码将会监听 `/ws` 路径的 WebSocket 连接请求,如果连接成功,则会不断地将客户端发送的数据返回给客户端。 3. 启动应用程序,WebSocket 服务器就已经运行了。 ## 实现 WebSocket 客户端 1. 创建一个控制台应用程序。 2. 在 `Program.cs` 文件中,编写一个简单的 WebSocket 客户端: ```csharp using System; using System.Net.WebSockets; using System.Text; using System.Threading; using System.Threading.Tasks; namespace WebSocketClient { class Program { static async Task Main(string[] args) { using (ClientWebSocket clientWebSocket = new ClientWebSocket()) { await clientWebSocket.ConnectAsync(new Uri("ws://localhost:5000/ws"), CancellationToken.None); while (true) { string message = Console.ReadLine(); byte[] buffer = Encoding.UTF8.GetBytes(message); await clientWebSocket.SendAsync(new ArraySegment<byte>(buffer), WebSocketMessageType.Text, true, CancellationToken.None); buffer = new byte[1024]; WebSocketReceiveResult result = await clientWebSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None); Console.WriteLine(Encoding.UTF8.GetString(buffer, 0, result.Count)); } } } } } ``` 上面的代码将会连接到 `ws://localhost:5000/ws` WebSocket 服务器,并不断地从控制台读取用户输入的数据,并将其发送给服务器。同时,它也会不断地从服务器接收数据,并将其打印到控制台。 以上就是使用 .NET Core 3.1 实现 WebSocket 服务器和客户端的基本步骤。你可以根据自己的需求进行修改和拓展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值