即使使用 DIBv5 转换,C# 图像在剪贴板中加载时也会失去透明度

如何解决即使使用 DIBv5 转换,C# 图像在剪贴板中加载时也会失去透明度

我正在尝试将图像文件加载到剪贴板中,然后使用 Ctrl+V 将其粘贴到 Discord 中。可以将 PNG 粘贴到 Discord 并保留透明度,因为我可以右键单击以在 Mozilla 或 Chrome 上复制图像,然后按 Ctrl+V 将其粘贴到 Discord 客户端中。查看剪贴板内容显示 Mozilla 在复制图像后存储 DeviceIndependantBitmapFormat17 数据格式。因此,我调整了帖子 Copying From and To Clipboard loses image transparency 中的函数,将 Image 对象转换为 DIBv5 数据,并以 DeviceIndependantFormat 名称将其加载到剪贴板中。

但是不支持透明度。然后将图像粘贴到 Discord 中时,所有透明区域都填充为黑色。 这是用于测试剪贴板中 PNG 加载的脚本。 DIB 标头构建为在复制图像时与 Mozilla 的 DIB 标头完全匹配(仅更改宽度、高度和像素数信息):

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;

namespace ClipboardDIBTest
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            // Loads a PNG with transparency in the clipboard.
            SetClipboardImage(Image.FromFile("./test.png"));
        }

        /// <summary>Loads an image into the DIBv5 format in the clipboard,/// under the `DeviceIndependantBitmap` format.</summary>
        static void SetClipboardImage(Image image)
        {
            Clipboard.Clear();
            DataObject data = new DataObject();
            using (MemoryStream dibV5MemStream = new MemoryStream())
            {
                Byte[] dibV5Data = ConvertToDIBv5(image);
                dibV5MemStream.Write(dibV5Data,dibV5Data.Length);
                data.SetData(DataFormats.Dib,false,dibV5MemStream);
                // The 'copy=true' argument means the MemoryStreams can be safely disposed after the operation.
                Clipboard.SetDataObject(data,true);
            }
        }
        
        /// <summary>Retrieves bitmap data from the image,ensuring an ARGB pixel format.</summary>
        static Byte[] GetBM32Data(Image image)
        {
            Byte[] bm32bData;
            // Ensure image is 32bppARGB by painting it on a new 32bppARGB image.
            using (Bitmap bm32b = new Bitmap(image.Width,image.Height,PixelFormat.Format32bppArgb))
            {
                using (Graphics gr = Graphics.FromImage(bm32b))
                    gr.DrawImage(image,new Rectangle(0,bm32b.Width,bm32b.Height));
                // Bitmap format has its lines reversed.
                bm32b.RotateFlip(RotateFlipType.Rotate180FlipX);
                bm32bData = GetImageData(bm32b);
            }

            return bm32bData;
        }

        /// <summar>Retrieves the pixel data from a bitmap.</summary>
        static byte[] GetImageData(Bitmap bmp)
        {
            Rectangle rect = new Rectangle(0,bmp.Width,bmp.Height);
            BitmapData bmpData = bmp.LockBits(rect,ImageLockMode.ReadOnly,bmp.PixelFormat);
            IntPtr ptr = bmpData.Scan0;
            int bytes = Math.Abs(bmpData.Stride) * bmp.Height;
            byte[] data = new byte[bytes];
            Marshal.Copy(ptr,data,bytes);
            bmp.UnlockBits(bmpData);
            return data;
        }

        /// <summary>Converts the image into a DIBv5 (Format17) data format.</summary>
        static Byte[] ConvertToDIBv5(Image image)
        {
            Byte[] bm32bData = GetBM32Data(image);
            Int32 width = image.Width;
            Int32 height = image.Height;

            // Header data built to match Mozilla's Format17 content when an image is copied.

            Int32 hdrSize = 124;
            Byte[] fullImage = new Byte[hdrSize + bm32bData.Length];
            //Int32 bV5Size;
            WriteIntToByteArray(fullImage,4,true,(UInt32)hdrSize);
            //Int32 bV5Width;
            WriteIntToByteArray(fullImage,(UInt32)width);
            //Int32 bV5Height;
            WriteIntToByteArray(fullImage,8,(UInt32)height);
            //Int16 bV5Planes;
            WriteIntToByteArray(fullImage,12,2,1);
            //Int16 bV5BitCount;
            WriteIntToByteArray(fullImage,14,32);
            //Int32 bV5Compression;
            WriteIntToByteArray(fullImage,16,0);
            //Int32 biSizeImage;
            WriteIntToByteArray(fullImage,20,(UInt32)bm32bData.Length);
            // These are all 0. Since .net clears new arrays,don't bother writing them.
            //Int32 bV5XPelsPerMeter = 0;
            //Int32 bV5YPelsPerMeter = 0;
            //Int32 bV5ClrUsed = 0;
            //Int32 bV5ClrImportant = 0;
            //Int32 Red/Green/Blue/Alpha masks
            WriteIntToByteArray(fullImage,40,0x000000FF);
            WriteIntToByteArray(fullImage,44,0x0000FF00);
            WriteIntToByteArray(fullImage,48,0x00FF0000);
            WriteIntToByteArray(fullImage,52,0xFF000000);
            // Int32 bV5CSType : "sRGB"
            WriteIntToByteArray(fullImage,56,1934772034);
            // Rest is all 0

            Array.Copy(bm32bData,fullImage,hdrSize,bm32bData.Length);
            return fullImage;
        }

        static void WriteIntToByteArray(Byte[] data,Int32 startIndex,Int32 bytes,Boolean littleEndian,UInt32 value)
        {
            Int32 lastByte = bytes - 1;
            if (data.Length < startIndex + bytes)
                throw new ArgumentOutOfRangeException("startIndex","Data array is too small to write a " + bytes + "-byte value at offset " + startIndex + ".");
            for (Int32 index = 0; index < bytes; index++)
            {
                Int32 offs = startIndex + (littleEndian ? index : lastByte - index);
                data[offs] = (Byte)(value >> (8 * index) & 0xFF);
            }
        }

        static UInt32 ReadIntFromByteArray(Byte[] data,Boolean littleEndian)
        {
            Int32 lastByte = bytes - 1;
            if (data.Length < startIndex + bytes)
                throw new ArgumentOutOfRangeException("startIndex","Data array is too small to read a " + bytes + "-byte value at offset " + startIndex + ".");
            UInt32 value = 0;
            for (Int32 index = 0; index < bytes; index++)
            {
                Int32 offs = startIndex + (littleEndian ? index : lastByte - index);
                value += (UInt32)(data[offs] << (8 * index));
            }
            return value;
        }
    }
}
  • 原始图像文件:

Original PNG image,with transparency.

  • 运行程序后,使用 Ctrl+V 在 Discord 中粘贴的图像:

Pasted result in Discord. Transparent regions are filled with black.

我在调试/重构过程中尝试了以下操作:

  • 从 Mozilla 复制图像(右键单击并复制),然后读取以 Format17 名称存储在剪贴板中的数据,并将其保存到文件中,然后再次将文件内容加载到剪贴板中.如果以 Format17 名称加载,则 Ctrl+V 不会发生任何事情(Discord 不会检测到任何要粘贴的内容)。但如果在 DeviceIndependantBitmap 下加载,则图像会正确粘贴到 Discord 中(透明)。
  • 使用上面提到的 post 中的相同代码以更简单的 DIB 格式加载图片,尽管它也提供了黑色背景。

我读过的其他帖子:

有人知道为什么会发生这种情况以及如何解决吗?谢谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-