生成带有一些透明部分的图像
我试图生成一个 32 x 32 像素的小正方形,中间有一个 10 x 10 的正方形透明间隙。
这就是我到目前为止所拥有的:
private Image CreatePicture(){
// Create a new Bitmap object, 32 x 32 pixels in size
Bitmap canvas = new Bitmap(32,32,System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
for(int i=10;i<21;i++){
for(int p=10;p<21;p++){
canvas.SetPixel(i,p,Color.Lime);
}
}
canvas.MakeTransparent(Color.Lime);
// return the picture
return canvas;
}
它很粗糙,不会是最终的“优化版本”,它只是一个粗糙的演示脚本。 问题是返回的图像不透明,而只是一个灰色框:(。
任何帮助表示赞赏。
迈克尔
更新: 我已经更新了脚本,将 PixelFormat 设置为 Alpha RGB 格式,它实际上接受该格式,并且在运行时不会出错。 现在,如果我删除“canvas.MakeTransparent(Color.Lime);” 它在中间显示了一个石灰框,它只显示了一个与灰色背景颜色相同的灰色框; 因此,似乎透明度得到了认可,但并没有隐含透明度!
private Bitmap CreatePicture(){
// Create a new Bitmap object, 50 x 50 pixels in size
Bitmap canvas = new Bitmap(82,82,System.Drawing.Imaging.PixelFormat.Format32bppArgb);
for(int i=10;i<71;i++){
for(int p=10;p<71;p++){
canvas.SetPixel(i,p,Color.Lime);
}
}
canvas.MakeTransparent(Color.Lime);
// return the picture
return canvas;
}
Im trying to generate a small square 32 by 32 pixels with a 10 by 10 squareish transparent gap in the middle.
This is what I have so far:
private Image CreatePicture(){
// Create a new Bitmap object, 32 x 32 pixels in size
Bitmap canvas = new Bitmap(32,32,System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
for(int i=10;i<21;i++){
for(int p=10;p<21;p++){
canvas.SetPixel(i,p,Color.Lime);
}
}
canvas.MakeTransparent(Color.Lime);
// return the picture
return canvas;
}
Its a it rough and not going to be the final "optimized version" its just a rough demo script. The problem is the returned image does not transparency instead its just a grey box :(.
Any help appreciated.
Michael
UPDATE:
I have updated the script with the PixelFormat set to an Alpha RGB format which it actually accepts without erroring on runtime. Now though if I remove the "canvas.MakeTransparent(Color.Lime);" line it shows a lime box in the middle with it it just shows a grey box the same colour as the grey background; so it seems as if transparency is being recognised just not implimenting the transparency!
private Bitmap CreatePicture(){
// Create a new Bitmap object, 50 x 50 pixels in size
Bitmap canvas = new Bitmap(82,82,System.Drawing.Imaging.PixelFormat.Format32bppArgb);
for(int i=10;i<71;i++){
for(int p=10;p<71;p++){
canvas.SetPixel(i,p,Color.Lime);
}
}
canvas.MakeTransparent(Color.Lime);
// return the picture
return canvas;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试在此处使用某种带有 alpha 通道 (...Rgba) 的格式。 另外,如果稍后输出为图像,请确保使用支持 Alpha 通道的图像格式,例如 PNG。
Try to use some format with an alpha channel (...Rgba) here. Also, if the output will be an image later, make sure you use an image format like PNG that supports alpha channels.
这取决于您将如何使用或显示图像。 PNG 和 Gif 是支持透明度的两种文件格式。
对于透明度的位图设置,我使用:
Graphics.FromImage(bitmap).FillRectangle(Brushes.Transparent, ...) 将我想要的区域设置为透明,然后将文件保存为 PNG 以获得图像透明度。
我还尝试了 MakeTransparent 方法并遇到了问题,Brushes.Transparent 是适合我的情况的解决方案。
希望这能为您提供研究方向。
最后一点,我创建的 Bitmap 对象仅具有宽度和高度,我没有指定像素格式或任何所谓的内容,即 bitmap = New Bitmap(width, height);
It depends on how your going to use or display the image. PNG and Gif are the two file formats that support transparency.
For the bitmap setting of transparency, i used:
Graphics.FromImage(bitmap).FillRectangle(Brushes.Transparent, ...) to set the area I wanted as transparent, then I saved the file out as a PNG to get an image with transparency.
I also tried the MakeTransparent method and ran into problems and the Brushes.Transparent was the solution that worked for my situation.
Hope this gives you a direction to look into.
One last note, I create the Bitmap object with width and height only, I don't specify the pixel format or whatever that's called i.e. bitmap = New Bitmap(width, height);
您可能需要将图像格式更改为 Format16bppArgb1555。
现在,您使用的是 Format32bppArgb 或 Format16bppRgb555,它是每像素 16 位,没有 Alpha 信息。 透明度需要存在 Alpha 通道或 Alpha 位。
You probably need to change the image format to Format16bppArgb1555.
Right now, you're using Format32bppArgb or Format16bppRgb555, which is 16 bits per pixel with no alpha information. Transparency requires an alpha channel or alpha bit to be present.
我不只是
SetPixel()
到 颜色.透明? 用MakeTransparent
代替 Color.Lime? 正如其他人所指出的; 您需要带有 Alpha 通道的像素格式。My don't you just
SetPixel()
to Color.Transparent? Instead of Color.Lime thenMakeTransparent
? And as others have noted; you need a pixel format with an alpha channel.MakeTransparent 函数确实有效。 您有“灰色补丁”,因为您的 TransparencyKey 不正确。 您会发现,如果将 TransparencyKey 属性设置为灰色块的任何颜色,则当显示图像时,灰色块确实会变得透明。 我得到的默认灰色补丁的颜色标记为“Control”,或者基本上是默认的 Windows 窗体背景颜色。 将您的 TransparencyKey 更改为您的灰色补丁(在我的例子中为 Control),您的问题将得到解决,无需将文件保存到硬盘驱动器。 队友的欢呼声。
The MakeTransparent function does work. You have "grey patches" because your TransparencyKey is not correct. You'll find that if you set your TransparencyKey property to whatever color that grey patch is, the grey patches will indeed become transparent when your image is displayed. The default grey patch I got was labeled as "Control" in color, or basically the default Windows Form background color. Change your TransparencyKey to whatever your grey patch is (in my case Control) and your problem will be solved, no saving of files to the Hard Drive neccessary. Cheers mate.