How can I draw legible text on a bitmap (Winforms)? - c#

I've written a photo viewer, and I want to superimpose text over the photo. I want the font or font color to make the text as legible as possible on top of the bitmap, no matter what the underlying bitmap looks like.
My current thinking is to take the region of the bitmap where the text will appear, and make some kind of "overall color" calculation for that area, and then set the font color to be something correspondingly contrasting.
However, this math is way over my head. Has anybody seen a method for making this type of "what's the average color of all of these pixels" calculation? Or is that not even the best approach?
EDIT: I'm moving the second portion of this to another question.

You can use this to calculate average color of a region of bitmap:
How to calculate the average rgb color values of a bitmap
Do you store your image as a Bitmap?
You can also draw an outlined text. For example, white text with black outline. This will make text visible on most of backgrounds:
How to Drawing Text with Outline onto Images?

Related

how to replace a pixel with a set of pixel c#

hi i'm making photo mosaic creator but i can't replace a pixel with a set of pixel if any one know how to do this in c# please help cause i'm beginner in c#
and i understand the idea but i can't implement in c#
the idea is we have a input photo and we have a data set of small photos and we create an empty output photo and we put inside here the small photo from the data set after we take the pixel average RBG color from the input photo and we take the average RBG color of the small photo then we replace the pixel with the photo that have the same RGB color of the pixel and sort the small photo's in the output photo
Well, you don't "replace a pixel with an entire picture" when building a mosaic digitally. Instead, you create a brand new output image that is much larger than the input. Then you think about a grid on the output so that each input pixel corresponds to a grid square in the output. You then copy an image into each grid square, checking the corresponding input pixel to find out what image is appropriate.
I have not used it, but WriteableBitmapEx is a class for creating bitmaps and has a SetPixel method.

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

Change Color For Parts of an Image

I am trying find a solution that would allow me change color for parts of an image.
For example lets say a picture of a chair.
A chair can have different frame or cushion colors.
I intend to let users select a frame or cushion and update the image.
We can only upload one chair image.
Any idea is much appreciated!
Detecting parts of the image to be modified
Detecting objects on images programatically is probably not the simplet thing to do ;). I suppose one of the good solutions to this problem was suggested by Collin O'Dell in his comment to the question. He suggested to use few images with manually separated parts of the image you want to recolor. Then, you can compose the final image from few different layers.
However, you can also keep the main image with all the objects and manually make some additional images, but only to keep masks of the objects (i.e. white pixels in places where the object is). You can then easily check which pixels should be recolored. This would allow you to paint directly on one image and avoid compositing.
Calculating the color
When you want to recolor the photograph, you probably want to be able to keep the shading effects etc. rather than covering all with the same solid color.
In order to achieve this, you can use HSV color space instead of RGB. To use this method you should:
read pixel's color (in RGB)
recalculate it to HSV
change Hue value of the color to get the desired color shade
recaluclate modified color back to RGB
set the new color of the pixel
For more information about HSV color space you can look here: http://en.wikipedia.org/wiki/HSL_and_HSV.

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.

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.

Categories