Enlarge image in XAML & keeping seperated pixels - c#

In a Windows Store App (C# / XAML) I'd like to show a 10x7 pixel grayscale image, but blown up so one can see the pixels. Unfortunately, by default XAML is blending neighbouring pixels like seen below, but I'd like to have sharp edges for each one so I can see where a pixel starts and where it ends. How can I achieve this?

Related

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.

Is there a way to crop and deskew a quadrilateral from an image in a Windows Store (WinRT) application?

I'm writing an application for the windows store that uses Canny Edge Detection to find the borders of a document on an image. I need to be able to crop this image once the corners are found. I can use the WriteableBitmapExtension methods to crop a rectangle, but the problem is that it will rarely be a rectangle, but rather a quadrilateral.
I read about something called Aforge that may be able to do it, but it doesn't support Silverlight/WinRT it looks like. I know this should be possible with OpenGL, but it would most likely require I change a large portion of my application. Are there any alternatives?
You could implement it with WriteableBitmapEx using Blit and n alpha mask for the region you want to crop. Just create the mask dynamically with the result from the Canny edge detection. Make sure all pixels you want to keep have an alpha value of 255 and the ones you want to crop have an alpha value of 0 in the mask bitmap. Then use the Blit method on the original image, supply the generated alpha mask bitmap as parameter and the BlendMode.Alpha as well. This won't really reduce the size of the original image but at least the unwanted pixels are gone.
Before the alpha masking you could already crop rectangular using the min, max of x and y from your edge detection result. This way the size is also reduced and your alpha masking should be faster as a bonus.

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.

Calculate source RGBA value from overlay

The title looks a bit weird, so here is a sample:
You will probably be familiar with the Windows 7 Explorer.
When you select something with the pressed left mouse button, you get a blue half transparent rectangle.
So the question is now:
It is possible to calculate the source RGBA color that was used to draw this rectangle?
The only things I know is the RGB value from the background and the RGB value with the blue overlay. Here is a screenshot from what I mean:
No, it ist not possible from a single image.
You need the target data over a black and a white image (best case).
After that, you need to use the first forumular in this article:
http://en.wikipedia.org/wiki/Alpha_compositing

How can I set an image to have a transparent background in C# without using Bitmap.MakeTransparent()?

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.

Categories