C# TextRenderer text black shadows - c#

I have a project the put random quotes on desktop wallpapers, you can position them, top or bottom. Anyhow, the text works great when using DrawText, but since it is required to have a custom alignment, LEFT, CENTER, or RIGHT.. I moved to using TextRenderer.
below is the screen shot, and if you notice, there is a transparent horizontal box along the screen with text.
but since Stackoverflow is crashing everytime I upload a HiRes image, I just resized it and just show this to you
notice the black shadows around the characters? How do I get rid of that?
-----------------
solved by adding
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

Related

Cannot make watermark image for Signature

I have a logo image that I need to embed in the signature. The logo is such like, a square with 4 parts & each of different part. I want to set watermark on the image, so the text is visible clearly. The code that I use is :
sap.Image = logoSign;
sap.ImageScale = 0.40f;
sap.Image.SetAbsolutePosition(100, 100);
//sap.Image.Transparency.SetValue(50, 0); // Only half image is visible
sap.GetAppearance().AddImage(logoSign);
It is scaled & placed properly. To achieve watermark effect, I add Transparency. When I made its value as (30,0) top left part of the square logo was visible. On making it 50, left half is visible, then I tried with 80, 100, 120, but the full logo is never visible. Elther 1/4th or 1/2 is only visible. The right part that is not showing up has Pink & yellow shades. Surprising is, when I comment the Transparency line, whole logo is visible.
Can anyone help me know, why is it acting like this. And ya, the logo with GIF is only working. PNG &/or JPG logo doesn't show up. Why so ?? I tried other image of jpg also, but no success.
#mkl, RESULTS shown in screenshots
Thanks
My advice would be to edit the image itself to a particular transparency level with the help of an editor and then use it. why do it in the code.

Find then hide drawn shape in image?

This may seem like a strange question, but I have a set of pictures I want to use as a fading screen saver, and I want each picture to have an accompanying quote, but each quote will be in a different place, relative to the picture.
Rather than coding a Rectangle for the area of each image, it would be easier just to draw a rectangle in the image, and have it drawn in there.
Now, the drawn shape would be a stark, uniform coloured border (lime green, for instance) because the colour doesn't appear in the pictures, and it would be on a solid coloured background, like black for instance.
My question is: If I draw a lime green rectangle on each image, how could I then, using C#, find that rectangle in the image, get the dimensions of it, and replace the lime green with the background colour, so in the end product, it'd look like the shape was never there to begin with?
I have not tried anything, I have no code to show, because it's an idea I had and though I'm sure it must be possible, I don't even know what to start searching for.
I hope this is possible, if it isn't, then I'll just draw a rectangle for each one, but that's a last resort. :)
Have you thought about using either the emgucv or aforge libraries? They are able to detect shapes in images quite easily. Though they don't detect edges perfectly, if all you want to detect is a rectangle, either one should work well.
Here are two tutorials on shape detection for emgu cv and aforge. Once you have the shape detected, then you can easily cut it out for the background. Say, for example, that you used the aforge Detection of quadrilaterals (in the link), then you could easily get the rectangles position and size by the calling of blobs[i].Rectangle; and create a rectangle with same size and position with picture background color.
If you need more clarification please feel free to comment. Nice idea!

Weird glitch with ASP.net drawing

I have a code that draws a few things. At the end, it draws a string. The string looks like it was copied from a text editor -- really nice! But weirdly, when I try to draw exactly same image only 10 pixels wider, the text gets blurry. When I say wider, I only mean like initializing a Bitmap with +10px width. Image still draws on the same area like those 10px were not there.
I already checked measureString. It seems fine. My first thought was that it measures wrong and shrink text width but that can't be case since I'm only passing text and font to measureString.
I already tried drawing the text then copying the whole Image to a new Bitmap only 10px wider (Image again stays the same width, the 10px are just blank space) but exactly same thing happens. I have way too much code to post it all. Any suggestions as to what could be the problem?
Example:
Thanks
You most probably forgot about a late-on width constraint which is keeping your final picture from expanding in width correctly: in your "resized" sample the drawn text is being shrunk, so logic (and the proportionally wider whitespace to the right of the "resized" sample) suggests you're actually trying to fit a wider picture in the same area.

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.

c# Draw Image (Scaled) to Graphics, does not interpolate correctly. Fixes?

I have an image that is 1px wide, and some height. I need to draw this image across the entire width of the control on it's OnPaint event. I get it to draw, however not correctly. It seems like when it stretches it, it doesn't actually fill all the pixels. As if the interpolation is off. Is there a way to say "stop being smart, just draw it already"? I see no InterpolationMode.Off or .None in the options for the graphics object.
I can confirm I actually drawing the full width by using an image of width X where X is the same width as the control. Then when it draws, it covers the full area as normal. However this control is resized all the time, and to save memory and all that jazz using 1px wide images is quite normal in the web world. This is for a desktop C# application though. Any ideas on how to fix this?
Ok I figured out the magic keywords:
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
This coupled with setting the Interpolation Mode to NearestNeighbour allows for a full block to be drawn.
Without setting the Interpolation mode, you get weird blending (expected).
Without setting the PixelOffsetMode, the nearest neighbour algorithm has no neighbour to compare to on a blank paint and therefore only draws half the image, for half the width. Setting it to offset half, moves everything over by -0.5px, and allows this algorithm to work for block textures.
InterpolationMode.NearestNeighbor is what you want to use in this case.

Categories