views:

380

answers:

1

Is there a way to manipulate alpha of an image using alpha from another image?

Suppose I have a Image and I want to alter its alpha starting opaque at left and totally transparent at right, currently I draw another image with LinearGradientBrush and set alpha of orginal image from second image by looping pixel by pixel, is there another way in Gdiplus, some image mask, or blending alpha of two images?

Conclusion: it seems there is no way in GDI+ to blend two images, only way seems to be the manual way by iterating thru pixels.

+1  A: 

I think you're correct in that you have to do this pixel-by-pixel. I've also searched for a more "pure" way to do it, but this is what I ended up with:

 public enum ChannelARGB
 {
  Blue = 0,
  Green = 1,
  Red = 2,
  Alpha = 3
 }

 public static void transferOneARGBChannelFromOneBitmapToAnother(
  Bitmap source,
  Bitmap dest,
  ChannelARGB sourceChannel,
  ChannelARGB destChannel )
 {
  if ( source.Size!=dest.Size )
   throw new ArgumentException();
  Rectangle r = new Rectangle( Point.Empty, source.Size );
  BitmapData bdSrc = source.LockBits( r, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb );
  BitmapData bdDst = dest.LockBits( r, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb );
  unsafe
  {
   byte* bpSrc = (byte*)bdSrc.Scan0.ToPointer();
   byte* bpDst = (byte*)bdDst.Scan0.ToPointer();
   bpSrc += (int)sourceChannel;
   bpDst += (int)destChannel;
   for ( int i = r.Height * r.Width; i > 0; i-- )
   {
    *bpDst = *bpSrc;
    bpSrc += 4;
    bpDst += 4;
   }
  }
  source.UnlockBits( bdSrc );
  dest.UnlockBits( bdDst );
 }
danbystrom
that is what i am already doing :(
Anurag Uniyal