C#图像透明度调整的方法

内容摘要
本文实例讲述了C#图像透明度调整的方法。分享给大家供大家参考。具体如下:


//定义图像透明度调整函数
public Bitmap PTransparentAdjust(Bitmap src,int num)
{
try
{
文章正文

本文实例讲述了C#图像透明度调整的方法。分享给大家供大家参考。具体如下:

//定义图像透明度调整函数
public Bitmap PTransparentAdjust(Bitmap src,int num)
{
  try
  {
   int w = src.Width;
   int h = src.Height;
   Bitmap dstBitmap = new Bitmap(src.Width, src.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
   System.Drawing.Imaging.BitmapData srcData = src.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
   System.Drawing.Imaging.BitmapData dstData = dstBitmap.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
   unsafe
   {
     byte* pIn = (byte*)srcData.Scan0.ToPointer();
     byte* pOut = (byte*)dstData.Scan0.ToPointer();
     byte* p;
     int stride = srcData.Stride;
     int r, g, b;
     for (int y = 0; y < h; y++)
     {
     for (int x = 0; x < w; x++)
     {
       p = pIn;
       b = pIn[0];
       g = pIn[1];
       r = pIn[2];
       pOut[1] = (byte)g;
       pOut[2] = (byte)r;
       pOut[3] = (byte)num;
       pOut[0] = (byte)b;
       pIn += 4;
       pOut += 4;
     }
     pIn += srcData.Stride - w * 4;
     pOut += srcData.Stride - w * 4;
     }
     src.UnlockBits(srcData);
     dstBitmap.UnlockBits(dstData);
     return dstBitmap;
   }
  }
  catch (Exception e)
  {
   MessageBox.Show(e.Message.ToString());
   return null;
  }
}

原图:

效果图(50%):

希望本文所述对大家的C#程序设计有所帮助。


代码注释

作者:喵哥笔记

IDC笔记

学的不仅是技术,更是梦想!