Generate image with some transparent parts - c#

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;
}

[...]Format16bppRgb555
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.

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);

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.

My don't you just SetPixel() to Color.Transparent? Instead of Color.Lime then MakeTransparent? And as others have noted; you need a pixel format with an alpha channel.

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.

Related

How do you replace a mask with a specific color?

I am using the following code snippet to remove the background from an image.
int[] bgColors = {203,255,240}
ImageMagick.MagickImage image = new ImageMagick.MagickImage("input.jpg")
ImageMagick.MagickImage combineMask= new ImageMagick.MagickImage("mask.jpg")
combineMask.Blur(7, 30);
image.Composite(combineMask, ImageMagick.CompositeOperator.CopyAlpha);
image.Settings.BackgroundColor = new ImageMagick.MagickColor(bgColors[0], bgColors[1], bgColors[2]);
image.Alpha(ImageMagick.AlphaOption.Remove);
It takes the MagickImage image and the MagickImage combineMask and outputs a combined image as linked below.
Original + Mask = Final
Previously, I never needed anything other than a plain white background (which this provides). Now, I would like to be able to set the color of the background. I started by passing a non (255,255,255) set of colors to the settings to image.Settings.BackgroundColor. This did not accomplish anything. I've been trying to investigate the MagickImage library (through their github, they don't really have very complete documentation), but it has remained inscrutable.
How could I get image.Alpha(ImageMagick.AlphaOption.Remove); to replace the removed section with the background color (or a passed color), or otherwise change the color of the background?
Solution
I've figured out a Solution that takes me 90% of the way there.
int[] bgColors = {203,255,240}
ImageMagick.MagickImage image = new ImageMagick.MagickImage("input.jpg")
ImageMagick.MagickImage combineMask= new ImageMagick.MagickImage("mask.jpg")
combineMask.ColorFuzz = new ImageMagick.Percentage(5);
combineMask.Transparent(ImageMagick.MagickColor.FromRgb(255, 255, 255));
combineMask.Opaque(ImageMagick.MagickColor.FromRgb((byte)0, (byte)0, (byte)0),
ImageMagick.MagickColor.FromRgb((byte)bgColors[0], (byte)bgColors[1], (byte)bgColors[2]));
image.Composite(combineMask, ImageMagick.CompositeOperator.Over);
Instead of using the Black/White mask and grabbing alpha channels, I am converting the white part of the mask to transparent, then converting the black part of the mask to the colors contained in the bgColors.
Now, because the formerly white area is now transparent, I cannot blur the image in order to create a smooth transition between mask and picture, which I still need to fix.

Make a WriteableBitmap Transparent in Windows Phone 8.1 C# WinRT Programmatically

Is it possible to add transparency to a WriteableBitmap in Windows Phone 8.1 using C# / WinRT programmatically?
I've referenced the WriteableBitmapEx library in my project for image resizing and blitting, which is quite useful, and it could also possibly help with adding transparency or alpha channels/layers, but the documentation is not very thorough.
It might be better to have a method without using the WriteableBitmapEx library to achieve this, but whatever works... I've been searching this for a while and there's not much information on a basic solution.
The idea is that a user can select an image or graphic, and pin it to their start screen, and the app would make the background color transparent...probably white or black, or even by using the first X/Y coordinate color of the image's background.
Have you tried using the transparent colour with your WriteableBitmapEx? For example to clear the entire bitmap use:
writeableBitmap = new WriteableBitmap(200, 200, 96, 96, PixelFormats.Bgra32, null);
writeableBitmap.Clear(Colors.Transparent);
If you want to edit specific pixels yourself - you need to access the PixelBuffer property. I'm sure WriteableBitmapEx does it all the time. So does my toolkit here. I can't recall the pixel format right now, but I think it's like ARGB or P-ARGB? I'm likely confused here, but basically your pixel buffer is an array that has data for all the row of pixels with each pixel defined by 4 bytes, so a 2x2 bitmap will have something like ARGBARGBARGBARGB bytes where each letter is one of four pixel component values (0-255 with 0 for no color and 255 for all RGB being white). R is Red, G is Green, B is Blue and A is Alpha - opacity or transparency. In P- or Premultiplied- pixel formats - all of RGB values are additionally multiplied by the Alpha channel value for performance reasons (IIRC).
You'll need to loop through all your pixels and set the ARGB values according to your rules to add transparency where needed, possibly un-pre-multiplying and pre-multiplying RGB as needed.

SharpDX and setting one color as transparent (directx9)

I have a C# capture application that captures screenshot of an application and draws it to another window. Now I would like to set one color (or range) of the screenshot to be transparent and then draw it like that.
I am drawing the screenshot as sprite to 3d (directx9) surface using SharpDX.
How can I do this transparency?
When converting your screenshot to a texture, your colors should be defined by four values R,G,B,A. Alpha channel is responsible for transparency, so you should check if a color has a value / range that you want to cut out and change it's alpha value to 0.
It was a bit odd situation. My alpha modified pictures did not show up when draw:ing.
It was because I did not specify alphablend mode in .Begin-statement.
And I did not find any other way to do it than going through the whole bitmap and changing the alpha value one pixel at the time.

How to get colors of particular area of a BMP image in c#?

I want to get the colors of the center bottom part of a BMP image. How this can be done, I need some techniques.
Check out the Bitmap class. It has a GetPixel() method which takes X/Y coordinates and returns the color of that pixel.
http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.getpixel%28v=VS.100%29.aspx
If this isn't enough, you'll have to describe more of what you are trying to accomplish.

How can I get and set pixel data in XNA?

I have a texture2D where I want to get the color of a specified pixel. Do something with it and put a new color in an other texture2D.
I will need to do this with all the pixels in the texture. How can I do this.
No pixel shader's please. It need to be in C#
The Texture2D class contains the GetData and SetData methods that should do exactly what you want.
I found my problem.
When I was trying to get the color of a pixel the Alpha value of the color was 0. This means that the color would be completely transparent. To solve it I just needed to change the Alpha value to 255.
I think that this happened because I am using an jpg file. Jpg file's do not support Alpha values.

Categories