C#——new Bitmap与clone读取PNG图片的相关问题

在C#中可以使用new Bitmap和Bitmap.clone的方法来获取一个Bitmap的备份,这两者实际上是有很大差别的。

代码如下:

Bitmap src = new Bitmap(@"C:\Users\Administrator\Desktop\樱花.png");
            Bitmap a = new Bitmap(src);
            BitmapData bgraData = a.LockBits(new Rectangle(0, 0, a.Width, a.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            Bitmap r = new Bitmap(a.Width, a.Height);
            BitmapData rData = r.LockBits(new Rectangle(0, 0, r.Width, r.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            byte* pSrc = (byte*)bgraData.Scan0;
            byte* pR = (byte*)rData.Scan0;
            for (int j = 0; j < a.Height; j++)
            {
                for (int i = 0; i < a.Width; i++)
                {
                    pR[0] = pSrc[0];//第一个像素点的b通道值为0
                    pR[1] = pSrc[1];
                    pR[2] = pSrc[2];
                    pR[3] = pSrc[3];
                    pR += 4;
                    pSrc += 4;
                }
            }
            r.UnlockBits(rData);
            a.UnlockBits(bgraData);

上述代码使用new Bitmap()的方法创建了src图像的备份对象,读取第一个像素的b通道值为0,alpha通道=0;

在看下面代码:

Bitmap src = new Bitmap(@"C:\Users\Administrator\Desktop\樱花.png");
            Bitmap a = (Bitmap)src.Clone();
            BitmapData bgraData = a.LockBits(new Rectangle(0, 0, a.Width, a.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            Bitmap r = new Bitmap(a.Width, a.Height);
            BitmapData rData = r.LockBits(new Rectangle(0, 0, r.Width, r.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            byte* pSrc = (byte*)bgraData.Scan0;
            byte* pR = (byte*)rData.Scan0;
            for (int j = 0; j < a.Height; j++)
            {
                for (int i = 0; i < a.Width; i++)
                {
                    pR[0] = pSrc[0];//第一个像素b通道值为25
                    pR[1] = pSrc[1];
                    pR[2] = pSrc[2];
                    pR[3] = pSrc[3];
                    pR += 4;
                    pSrc += 4;
                }
            }
            r.UnlockBits(rData);
            a.UnlockBits(bgraData);

上述代码使用src.clone()来创建了src的备份图像对象,但是读取第一个像素的b通道值为25,这里跟第一种方法就出现了偏差。

最后放上原图:


这张原图的第一个像素rgba的值分别为25,25,25,0

我们可以看到上述两种方法只有第二种是对的,因此在读取有透明度通道的PNG对象时,使用clone()方法,最好不要使用new Bitmap方法。

这里给自己的踩坑做个记录。

要在C#中使用Bitmap绘制半透明的PNG图片,可以使用Graphics类的DrawImage方法,并设置透明度。下面是一个示例: ```csharp using System; using System.Drawing; class Program { static void Main() { // 创建一个Bitmap对象 Bitmap bitmap = new Bitmap("path_to_png_file.png"); // 创建一个具有透明背景的Bitmap对象 Bitmap targetBitmap = new Bitmap(bitmap.Width, bitmap.Height); // 设置Graphics对象的CompositingMode为SourceOver以支持透明度 using (Graphics graphics = Graphics.FromImage(targetBitmap)) { graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver; // 设置透明度(0为完全透明,255为不透明) float opacity = 0.5f; // 创建颜色矩阵并设置透明度 ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.Matrix33 = opacity; // 创建颜色矩阵属性 ImageAttributes imageAttributes = new ImageAttributes(); imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); // 绘制具有透明度的PNG图片到目标Bitmap上 graphics.DrawImage(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height), 0, 0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel, imageAttributes); } // 保存目标Bitmap图片文件 targetBitmap.Save("path_to_output_file.png"); Console.WriteLine("半透明PNG图片绘制完成。"); } } ``` 在这个示例中,你需要将 `"path_to_png_file.png"` 替换为你实际的PNG图片路径。首先,创建一个源Bitmap对象并加载PNG图片。然后,创建一个目标Bitmap对象,它具有与源Bitmap相同的尺寸。 通过设置Graphics对象的CompositingMode属性为SourceOver,我们启用了透明度支持。然后,我们通过创建一个ColorMatrix对象,并将其第三行的值设置为所需的透明度来设置透明度。接下来,我们创建了一个ImageAttributes对象,并使用SetColorMatrix方法将ColorMatrix应用于图像。 最后,使用Graphics对象的DrawImage方法将具有透明度的PNG图片绘制到目标Bitmap上,并保存目标Bitmap图片文件。 请确保提供的PNG图片具有透明度(即图片中的某些区域是透明的),以便在绘制时产生半透明效果。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Trent1985

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值