说谎的人 2021-04-01 13:31 采纳率: 0%
浏览 29

C# 图片加水印 上传到linux 服务器上报错

"ClassName": "System.Runtime.InteropServices.MarshalDirectiveException",

        "Message": "Cannot marshal 'parameter #1': Invalid managed/unmanaged type combination (Marshaling to and from COM interface pointers isn't supported).",

        "Data": null,

        "InnerException": null,

        "HelpURL": null,

        "StackTraceString": "   at System.Drawing.SafeNativeMethods.Gdip.GdipLoadImageFromStream(IStream stream, IntPtr& image)\n   at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)\n   at Erp.api.Controllers.SalOrderController.WaterMarkImage(IFormFile file) in D:\\Erp\\New.api\\Controllers\\SalOrderController.cs:line 319",

        "RemoteStackTraceString": null,

        "RemoteStackIndex": 0,

        "ExceptionMethod": null,

        "HResult": -2146233035,

        "Source": "System.Drawing.Common",

        "WatsonBuckets": null

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-23 18:57
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    要解决这个问题,首先确保你的应用程序已经正确安装了GDI+库。如果没有,请通过以下命令在命令提示符中安装:

    dotnet new console --install Microsoft.DirectWrite.Microsoft.DirectWrite.dll
    

    然后,你可以使用以下代码来加载图片并添加水印:

    using System;
    using System.IO;
    
    class Program
    {
        static void Main(string[] args)
        {
            string imagePath = @"path_to_your_image.jpg"; // 替换为实际的图像路径
            string watermarkText = "Your Watermark Text Here"; // 替换为你想要显示的文字
    
            using (var stream = File.OpenRead(imagePath))
            {
                var image = Image.FromStream(stream);
                var bounds = image.Size;
                int width = bounds.Width;
                int height = bounds.Height;
                var bitmap = new Bitmap(width, height);
    
                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        var pixel = image.GetPixel(x, y);
                        bitmap.SetPixel(x, y, Color.FromArgb(pixel.A, pixel.R, pixel.G, pixel.B));
                    }
                }
    
                using (var streamWatermark = File.Create("watermark.png")) // 创建一个名为"watermark.png"的新文件以保存水印
                {
                    bitmap.Save(streamWatermark, ImageFormat.Png);
                }
    
                // 将水印插入到原始图像上
                bitmap.DrawRectangle(Pens.Black, 0, 0, width, height);
    
                // 添加文本水印
                bitmap.DrawString(watermarkText, new Font("Arial", 20), Brushes.Black, 10, 10);
    
                // 保存最终图像
                using (var streamOutput = File.Create("output.png"))
                {
                    bitmap.Save(streamOutput, ImageFormat.Png);
                }
            }
        }
    }
    

    这段代码首先从指定的图像文件中读取像素数据,并将这些数据用于创建一个新的、大小相同的位图。接着,它遍历位图的每个像素点,将它们重命名为与原始图像不同的颜色。然后,它将这个新的位图保存到名为"watermark.png"的新文件中。

    接下来,它打开包含水印图像的新文件,并将其内容复制到输出图像中。最后,它在输出图像上添加文本水印。

    注意:这段代码假设你已经有一个名为"watermark.png"的水印图像文件,并且你知道你想添加的文本水印的位置和大小。如果你不确定如何获取这些信息,可能需要进一步调整代码以适应你的具体需求。

    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵