I have 2 images, foreground and background. I want to combine the 2 using an alpha mask image such that black = use background pixel, white = use foreground pixel and grey is an alpha blended mix of the 2.
The foreground and background images are fixed, while the mask image is created on the fly and then moved as required to create an animation.
Is there any performant way to do this using the standard library? I can use LockBits and a loop but I'd like something faster, if such a thing exists.
Related
I am trying the put a logo onto my object. I have a png image I want to use as a mask and two colors, one for the logo and one for the background.
So far I have managed to create two Materials for the same submesh, with one being the transparent image and the other being the background:
The color of the logo isn't changable though, as it is the image that is being displayed.
Is there a way to use the png as a mask, but change the color that is used?
Will I need to write a shader, or have I missed something in the material inspector?
Thanks in advance :)
Using two materials for the same submesh is a terrible idea, it has very low performance, it is not like having two passes in the same shader.
What you can do is creating a new shader that receives two textures:
One texture will have only 0 in the space for the logo and white in the other places
The other texture will be white in the logo and have 0 in the other pixels
Then you will have two color properties:
One will multiply with your "background" texture
The other will multiply with your "Logo" texture
Then Add both textures and assign them to the Albedo node.
You can do the same using a blend mask:
You will have a mask Map (basically it`s the Alpha channel) and put 0 in all places but where the logo pixels are.
Also you will need 2 colors properties.
Then use the blend node in the ShaderGraph to merge both textures.
Using the Blend mask you don't need the other two textures, only the mask and you can create maps in the ShaderGraph with the color properties.
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.
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.
I want to set an image to have a transparent background, but I do not want to replace all pixels of a specific colour with transparency.
To be more specific, the image is a thumbnail image for a folder, obtained via IShellItemImageFactory.GetImage. This gives me a Bitmap, as displayed in Windows Explorer thumbnail view, but the background is solid white.
I can use Bitmap.MakeTransparent on it, and that will work in most cases, but in any cases where the thumbnail image contains white itself (for example, a folder that contains images, which include white colours).
Incidently, this is the first time in over 10 years as a developer that, after googling my question, I have not found an answer anywhere, and I've actually had to ask it myself. (I think that means I just levelled up! Yippee, I am now a level 2 developer...)
Use flood-fill algorithm to fill pixels of the same color from the OUTSIDE as you need it. It is something similar to magic wand in photoshop.
http://en.wikipedia.org/wiki/Flood_fill
What I would do is flood-fill with some obscure color (Magenta always does it for me), then replace that color with transparent (I don't know if flood filling with transparent pixels is feasible).
So what you're getting from IShellItemImageFactory.GetImage is a composite image that contains the original image on a white background? I suspect the white background is there if the image doesn't have the same aspect ratio as the thumbnail size. For example, if you ask for a 96x96 thumbnail of a 640x480 image, there's going to be some white space at top and bottom.
If that's the case, you have a problem. You can't differentiate between white pixels that are contained in the image, and white pixels that are added by GetImage.
There are a few things you could do. You could load the image and resize it yourself. That's probably the easiest. You'd want to maintain your own thumbnail cache then.
Or you could examine the image returned by GetImage to look for white space on the sides. Basically, if every pixel on a row (or column) is white, then make that row (or column) transparent. It's a little more complicated than that (the NBA logo, for example). But that's essentially what you'd want to do.
I am processing images to change their color from black to red, blue, green etc based on the requirement. I use SetPixel methods to change color of each pixel of the image from black to say red.
It works mostly fine except the borders and some curves within the image. Let's say I've circled image filled with black color. Circled image color is changed but still when I zoom, I can see blackish dots around border which is not completely replaced with red color. I tried to dig around and found that it has something to do with anti-aliasing.
Has anything faced similar problem or have thoughts/suggestions on how to fix this issue?
Many thanks in advnace for your help!
Regards,
Tanush
It can be related with anti-aliasing. Anti-aliasing essence is that the more pixel is closer to the edge (boundary of something) the more pixel color is blended with background color (or we can say that it is more 'transparent').
So the problem may be that you need not only to replace source color to destination color, but also pixels which were blended from source color to background color.
To achieve this you need:
1) Run edge detection algorithm of some kind - it may be simple or advanced as you want.
2) If pixel is near edge and pixel is near other pixel of your source color, then calculate it's opacity (1-transparency) factor- which will be
opacity = (pixel_color-background_color)/(source_color-background_color)
3) Now calculate your color to which you must replace current anti-aliased pixel:
new_color = background_color * (1-opacity) + opacity * target_color
And put this new_color instead of antialiased pixel.
In summary:
You need to detect antialiased pixels and replace them with your version of antialiased pixels.
Hardest part of algorithm is detection of antialiased pixels - because you can't be sure that you found all edge pixels with 100% probability. Also you can't be sure was pixel antialiased or was just made initially of such color). Because of this you may get some color noise in final product. But in any case it should be better than just sit and wait :)
good luck