2011-07-20 61 views
0

我正在尝试创建一个图像覆盖图,其大小与具有透明部分的较低图像相同。我遇到的问题是叠加层的透明部分在画布上显示为黑色,或者从未真正设置为透明。 叠加是视图模型的属性:WPF中的图像叠加?

this.Logger.Trace("overlay get start"); 
Bitmap bmp = new Bitmap(this.imageWidth, this.imageHeight, PixelFormat.Format8bppIndexed); 


System.Drawing.Imaging.ColorPalette myPalette = bmp.Palette; 
for (int x = 0; x < 256; x++) 
{ 
    myPalette.Entries[x] = System.Drawing.Color.FromArgb(x, x, x); 
} 
bmp.Palette = myPalette; 

// Create a BitmapData and Lock all pixels to be written 
BitmapData bmpData = bmp.LockBits(
        new Rectangle(0, 0, bmp.Width, bmp.Height), 
        ImageLockMode.WriteOnly, 
        bmp.PixelFormat); 

for (int iRow = 0; iRow < this.imageHeight; iRow++) 
{ 
    Marshal.Copy(this.overlay, (iRow * this.imageWidth), bmpData.Scan0 + (iRow * bmpData.Stride), this.imageWidth); 
} 

// Marshal.Copy(this._overlay, 0, bmpData.Scan0, this.imageWidth * this.imageHeight);//this._overlay.Length); 

// Unlock the pixels 
bmp.UnlockBits(bmpData); 

bmp.MakeTransparent(Color.FromArgb(255, 255, 255)); 
bmp.MakeTransparent(bmp.GetPixel(0, 0)); 
bmp.MakeTransparent(bmp.GetPixel(1, 1)); 

this.Logger.Trace("overlay get end"); 

MemoryStream ms = new MemoryStream(); 
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); 
BitmapImage bImg = new System.Windows.Media.Imaging.BitmapImage(); 

bImg.BeginInit(); 
bImg.StreamSource = new MemoryStream(ms.ToArray()); 
bImg.CreateOptions = BitmapCreateOptions.PreservePixelFormat; 
bImg.CacheOption = BitmapCacheOption.Default; 
bImg.EndInit(); 

ms.Close(); 

imagesRec++; //for debug purposes 

return bImg; 

这里是XAML:

<StackPanel Grid.Column="1" > 
    <Canvas DragDrop:DragDropManager.DropTargetAdvisor="{StaticResource targetAdvisor1}" Name="CanvasObj" > 
     <Image x:Name="CameraImage" Source="{Binding CameraImage}" VerticalAlignment="Top" HorizontalAlignment="Center" ></Image> 
     <Image x:Name="Overlay" Source="{Binding Overlay}" VerticalAlignment="Top" HorizontalAlignment="Center" ></Image> 
    </Canvas> 
</StackPanel> 
+5

为什么不只是使用已设置透明度的PNG图像? –

回答

2

有没有必要做这一点;使用alpha图层的PNG格式图像,其余部分将“正常工作”。

+0

是的,这就是我问的 –

+0

是的,我看到了(你应该已经回答了!:)。这真的是唯一有意义的东西......不知道为什么会跳过这么多的篮球。 –