Weird glitch with ASP.net drawing - c#

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.

Related

C# TextRenderer text black shadows

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;

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 do I get the dimensions of the ImageRectangle in PictureBox?

Background
I want to be able to get the drawn dimensions of a zoomed image inside the picturebox (I'll explain below).
The PictureBox.ImageRectangle property seems to be exactly what I'm looking for because it shows the height and width of the resized image, as well as it's relative top, left position inside the control.
The trouble is PictureBox.ImageRectangle is private and so I can't read the values without using reflection (which is obviously not ideal).
Actual Question
My question is, is there another way that I can easily get to these values without writing a method to calculate what the values "ought" to be? I can do that easily enough, but I feel I'd be reinventing the wheel.
Context:
I'm writing a simple image processing app in C# and one of the things it has to do is allow the user to draw a selection around a portion of the image (a lot like the Marquee tool in Photoshop).
I need to know the dimensions of the rendered image so I know where to set the bounds of my marquee tool and also to translate the values of the drawn rectangle to points on the scaled bitmap inside the control.
My answer look simple so maybe I'm missing something, but I think Control.DisplayRectangle suits your need.
EDIT
OK, missed the point; however see How to get the value of non- public members of picturebox?
if you want to access dimension of the image in picture box you can use
GraphicsUnit units = GraphicsUnit.Point;
RectangleF imgRectangleF = pictureBox1.Image.GetBounds(ref units);
Rectangle imgRectangle = Rectangle.Round(imgRectangleF);

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.

c# GDI+ rendering problem

Not sure if anyone else has seen this, but I am rendering a custom control that has zoom functionality. Lets say for simplicity sake that I am just drawing one rectangle with border width = 1 in the viewable area of the control. When I alter the zoom (graphics.ScaleTransform()) the rectangle's borders take on different sizes (sometimes the same, mostly different). This makes my control look ugly when the user zooms in or out. I'm sure it's something really simple but i'm struggling to fix it. Any help appreciated!
thanks,
Thornza
Yes, the pen width gets scaled as well. Create a pen with a Width = 0. That will always be one pixel wide, regardless of the ScaleTransform.

Categories