1
比方說,我有兩個圖像,一個在另一個之上。我想頂端圖像的像素的α值的一部分設置爲零,因此我得到這種效果如下所示:設置一些像素的alpha爲零
棕色是頂部圖像,以及藍色水是底部圖像。最終,我要做的是當用戶觸摸iPad屏幕時,頂部圖像的像素的alpha通道變爲零,以便他們可以用指尖進行基本繪製並顯示藍色的水圖像。
我有一個函數,可以檢查計數它的像素的alpha等於零的圖像。我也有一個函數,將像素的alpha值設爲零,但我認爲它不起作用。 我可以讀取alpha值,並設置alpha值。然後我可以重新讀取alpha值以確認它已設置。但是當我設定新的圖像時,我看不出有什麼不同!
這裏是一個Dropbox的鏈接到我的示例項目:
https://www.dropbox.com/s/e93hzxl5ru5wnss/TestAlpha.zip
而且下面是源:
Water = new UIImageView(this.Bounds);
Water_OriginalImage = UIImage.FromFile("Media/Images/Backgrounds/WaterTexture.png");
Water.Image = Water_OriginalImage;
this.AddSubview(Water);
Dirt = new UIImageView(this.Bounds);
Dirt_ModifiableImage = UIImage.FromFile("Media/Images/Backgrounds/DirtBackground.png");
Dirt.Image = null;
this.AddSubview(Dirt);
CGImage image = Dirt_ModifiableImage.CGImage;
int width = image.Width;
int height = image.Height;
CGColorSpace colorSpace = image.ColorSpace;
int bytesPerRow = image.BytesPerRow;
int bitmapByteCount = bytesPerRow * height;
int bitsPerComponent = image.BitsPerComponent;
CGImageAlphaInfo alphaInfo = image.AlphaInfo;
// Allocate memory because the BitmapData is unmanaged
IntPtr BitmapData = Marshal.AllocHGlobal(bitmapByteCount);
CGBitmapContext context = new CGBitmapContext(BitmapData, width, height, bitsPerComponent, bytesPerRow, colorSpace, alphaInfo);
context.SetBlendMode(CGBlendMode.Copy);
context.DrawImage(new RectangleF(0, 0, width, height), image);
// Console output from this function call says "alpha count = 0"
CountZeroedAlphas(BitmapData, Dirt_ModifiableImage);
RemoveSomeAlpha(BitmapData, Dirt_ModifiableImage);
// Console output from this function call says "alpha count = 2000". So it seems that I have set the alpha of 2000 pixels to zero...
CountZeroedAlphas(BitmapData, Dirt_ModifiableImage);
// Set the Dirt Image equal to the context image, but I still see all top image, I dont' see any bottom image showing through.
Dirt.Image = UIImage.FromImage (context.ToImage());
// Free memory used by the BitmapData now that we're finished
Marshal.FreeHGlobal(BitmapData);
public void CountZeroedAlphas(IntPtr bitmapData, UIImage Image)
{
int widthIndex = 0;
int heightIndex = 0;
int count = 0;
while (widthIndex < Image.Size.Width)
{
while (heightIndex < Image.Size.Height)
{
PointF point = new PointF(widthIndex, heightIndex);
var startByte = (int) ((point.Y * Image.Size.Width + point.X) * 4);
byte alpha = GetByte(startByte, bitmapData);
if (alpha == 0)
count++;
heightIndex++;
}
widthIndex++;
}
Console.WriteLine("alpha count = " + count);
}
public void RemoveSomeAlpha(IntPtr bitmapData, UIImage Image)
{
int widthIndex = 0;
int heightIndex = 0;
while (widthIndex < Image.Size.Width)
{
while (heightIndex < Image.Size.Height)
{
PointF point = new PointF(widthIndex, heightIndex);
var startByte = (int) ((point.Y * Image.Size.Width + point.X) * 4);
ZeroByte(startByte, bitmapData);
heightIndex++;
}
widthIndex++;
}
}
public unsafe byte GetByte(int offset, IntPtr buffer)
{
byte* bufferAsBytes = (byte*) buffer;
return bufferAsBytes[offset];
}
public unsafe void ZeroByte(int offset, IntPtr buffer)
{
byte* bufferAsBytes = (byte*) buffer;
bufferAsBytes[offset] = 0;
}