RichTextBox: Transparent Background vs. Font Quality in Bitmap - c#

I created a RTF control pretty similar to the one described here.
Works pretty fine. Sometimes i have the control draw its contents directly to the device context, sometimes i use an offscreen bitmap which i draw to the device context myself, depending on the situation.
Now i needed the control to have a transparent background so i've overriden the CreateParams method called on initalising the base clss and added createParams.ExStyle |= 0x20; which boils down to one of the extended window styles, WS_EX_TRANSPARENT.
At this point, transparency works like a charm. But in contrast to before, setting this option makes drawing to the offscreen bitmap unusable, since the font quality suffers. Note, that only when drawing to the bitmap the font quality turns bad, drawing to the device context of the control is still working although creating the offscreen bitmap uses the same routines, just with a different device context.
As you can see, the font on the left is perfectly fine, while on the right side, the font get messy, too bold and it seems like it's drawn over several times with the coordinates not matching.
Commenting out the line added for transparency makes the bitmap return to normal but also yields in a white background for everything drawn, making me loose transparency.
Any ideas how to get a successfull combination of the two desired effects?

Related

cloned aRGB images from imagelist object fidelity

I note first that I'm working with VS 2010 Express.
My plan was to get cloned 32bppaRGB images, in this case 64x64, from an imagelist, draw on them, and append them to the imagelist.
I soon noticed that the clones, with no such drawing or
any other changes made, appeared differently from the originals
when seen attached to items in the listview. They appear
as if some alpha multiplication blending with some level of grey or who knows what color was done, but areas that are fully transparent are not affected.
But since this is object cloning, talk of anything happening in a graphics context should be nonsense. Instead I am tempted to guess this is another
case of microslack.
the original on the left is color A190, 219,215,114
I guess my plan could be fulfilled by using ownerdraw, or compositing images with a graphics lib, but this issue seems really odd, and I'd like to know what's going on.
to avoid that annoying issue with fading/greying images added via imagelists property page.
imageList1.Images.Add(Properties.Resources.png0);//8 bit per channel aRGB
I tried pngs created with a few different tools ie ImageMagick, paint.net, GIMP.
the routine
void foo {
listView1.Items.Add("png0", 0);
Image imgclone = (Image)imageList1.Images[0].Clone();
imageList1.Images.Add(imgclone);
listView1.Items.Add("png0clone", 1);
}
Thanks

How to create anti-aliased text on a Bitmap with ExtTextOut?

In my WinForm app I draw into a System.Drawing.Bitmap. I create fonts from a LOGFONT and draw using the GDI function ExtTextOutW. However the output is terrible. It has bad jaggies and looks like the antialiaser was on LSD. Reading around this seems a common issue - is there a resolution?
If I use:
lf.lfQuality = FontQuality.NONANTIALIASED_QUALITY
when I create the font then the horrible jaggies go away but of course there is no antialiasing.
Is there a way to create smooth text in a Bitmap with ExtTextOutW?
It is possible but a bit tricky and it can't have transparent background.
You will need to:
Create in-memory bitmap buffer that is compatible with display device context (IntPtr.Zero handle)
Fill the buffer background with solid color or other background
Render the text into the memory bitmap
Copy from in-memory bitmap to image device context (BitBlt)
See GDI text rendering to image for more details.

Can't get gradient transparent border to display correctly

I am having trouble getting my Form background image to display correctly with a gradient transparency. That is, the edge of the image (or anywhere for that matter) has a fading or non-255 Alpha value. The result is that everywhere the Alpha is NOT 255 (or 0 - I can't remember) I see my background color. Here is a screen shot:
What I have in this setup is a 24bit bmp file with a green background that I'm trying to key out via the Form.TransparencyKey property:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Color key = ((Bitmap)this.BackgroundImage).GetPixel(0, 0);
this.TransparencyKey = key;
}
I have also tried setting the Form's BackColor property to the same key value but that did not make a difference.
I also tried saving my image as a png with transparency and keying out the entire form background but that didn't work either.
I know this is possible somehow, I've seen applications like Photoshop use this for a decade. I'm on WinXP Pro if that matters.
Anyone have experience with this and have an idea what step I'm missing or doing incorrectly?
The WinForms implementation of Form only permits a solid colour to be used as the transparency key. It's not possible to have varying levels of transparency.
Most apps with splash screens like yours take a screenshot of the desktop (within the bounds that their splash screen will occupy) and then composite their splash screen on top of this screenshot. They then display the resulting bitmap to the user, giving the impression of an alpha gradient.
I've had my share of failure time with exactly that.
I found that WinForms is simple weak at handling this.
Here is some solutions that you might find useful:
http://www.c-sharpcorner.com/UploadFile/scottlysle/XparentFormsCS10282007212944PM/XparentFormsCS.aspx
Link
http://www.vcskicks.com/splash-screen.php

screen color filtering

I want to create a filter over a specific area of the screen to perform filtering opertions.
Examples what a filtering opertion might be:
    - inverting (e.g. change black pixel to white pixels, red to cyan)
    - masking pixels (e.g. mask = ff0000; input c79001 -> c70000)
    - operations like photoshop's layer effects
Here is an example of what it should look like:
http://img443.imageshack.us/img443/1462/overlayk.png
Does anyone know how to perform this under Windows OS.
(my prefered language is C#)
Thanks!
Depending on how fast you need the "filter" to update, a quick and hacky way is just to get a screenshot using CopyFromScreen while your filter window is invisible, apply the filter to the image data, and then set the filter window to display the image data.
If you want to do it without having to hide the window first, you'll probably need to do something like http://www.codeproject.com/KB/system/snapshot.aspx where you capture individual windows.
An even trickier but potentially faster thing to do, and requiring nearly complete use of p/invoke win32 calls, would be to not have a window at all, get the required capture windows based on their coordinates, capture the images as above, and then draw directly to the screen DC.
To clarify: you want an area of the desktop, not just within the bounds of your window, to be under your control allowing you to apply a per-pixel filter. If that's the case, I think what you need is DirectDraw using the XNA libraries. WPF MAY get you what you need, but WinForms will most likely not. There are third party tools as well.
If you want this capability only within the bounds of your application's window, for instance in a drawing application, it gets far easier. Every class in the Windows.Forms namespace that inherits from Control exposes a CreateGraphics() method. This method returns an object representing a drawing surface covering the screen area of the control, and is the basis for just about anything you want to do on a window involving custom graphics (and internally, it's used to draw the controls in the first place).
Once you have the Graphics object, you can draw Images on it. A popular method of drawing custom graphics like animations or games is to do the actual drawing on a Bitmap object (derived from the abstract Image) and then when you're done, draw the Bitmap on the Graphics area. This is done to reduce flicker; if the graphics area is shown to the user while it is being drawn on, the user will only see the complete image for a split second before it is "wiped" and redrawn, and shapes drawn halfway through will be there one moment and gone the next as they wait to be drawn. Drawing to a bitmap, then showing the Bitmap on the screen when you're done, means the user sees a complete image at a time.
You can extend this using transparency features to create multi-layered images. Have a Bitmap for every layer you wish to manipulate. Work on them seperately, then draw each of them, in their proper order from back to front, onto a master Bitmap, and draw that Bitmap on the screen. This allows you those PhotoShop-type manipulations where a part of the image is one layer, and can be manipulated independently of all others.
As for per-pixel filtering, Bitmap objects expose GetPixel() and SetPixel() methods, which allow you to grab the color of a single pixel, perform a filter calculation, and re-draw it. This process will be totally unaccelerated, and so limited by your CPU speed, but allow very fine control of your image, or repetitive tasks like your filters.

Taking screenshots in Windows Vista, Windows 7, with transparent areas outside the app region

I am trying to take a screenshot of an application and I would like to make the parts of the rectangle that are not part of the applications region be transparent. So for instance on a standard windows application I would like to make the rounded corners transparent.
I wrote a quick test application which works on on XP (or vista/windows 7 with aero turned off):
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
// Just find a window to test with
IntPtr hwnd = FindWindowByCaption(IntPtr.Zero, "Calculator");
WINDOWINFO info = new WINDOWINFO();
info.cbSize = (uint)Marshal.SizeOf(info);
GetWindowInfo(hwnd, ref info);
Rectangle r = Rectangle.FromLTRB(info.rcWindow.Left, info.rcWindow.Top, info.rcWindow.Right, info.rcWindow.Bottom);
IntPtr hrgn = CreateRectRgn(info.rcWindow.Left, info.rcWindow.Top, info.rcWindow.Right, info.rcWindow.Bottom);
GetWindowRgn(hwnd, hrgn);
// fill a rectangle which would be where I would probably
// write some mask color
g.FillRectangle(Brushes.Red, r);
// fill the region over the top, all I am trying to do here
// is show the contrast between the applications region and
// the rectangle that the region would be placed in
Region region = Region.FromHrgn(hrgn);
region.Translate(info.rcWindow.Left, info.rcWindow.Top);
g.FillRegion(Brushes.Blue, region);
}
When I run this test app on XP (or Vista/Windows 7 with Aero off), I get something like this, which is great because I can eek an xor mask out of this that can be used later with BitBlt.
removed dead Imageshack link - Screenshot
Here is the problem, on Vista or Windows 7 with Aero enabled, there isn't necessarily a region on the window, in fact in most cases there isn't. Can anybody help me figure out some way to build a mask like this on these platforms?
Here are some of the approaches I have already tried...
1. Using the PrintWindow function: This doesn't work because it gives back a screenshot taken of the window with Aero off and this window is a different shape from the window returned with Aero on
2 Using the Desktop Window Manager API to get a full size thumbnail: This didn't work because it draws directly to the screen and from what I can tell you can't get a screenshot directly out of this api. Yeah, I could open a window with a pink background, show the thumbnail, take a screenshot then hide this temporary window but thats a horrible user experience and a complete hack I would rather not have my name on.
3. Using Graphics.CopyFromScreen or some other pinvoke variant of this: This doesn't work because I can't assume that the window I need information from is at the top of the z-order on the screen.
Right now, the best solution I can think of is to special case Aero on Windows 7 and Vista to manually rub out the corners by hard coding some graphics paths I paint out but this solution would suck since any application that performs custom skinning will break this.
Can you think of another or better solution?
If you are here, thanks for taking time to read this post, I appreciate any help or direction that you can offer!
If you are looking for a finished application, there is 7capture, which captures also the translucency, so images can be saved to PNG format for later compositing.
EDIT:
The original question and comments indicate you are looking to produce a region on Windows Vista/7 that you can then use to mask out parts the captured image, as is done with Windows XP and non-Aero UIs. Using a region is not going to give you the result you are looking for, since the window outline is not computed as a region, but as an image with variable transparency - RGBA. The Alpha channel in that image is your mask, but it's not an on-off mask like a region, but a gradual mask with a range of values from pixels being fully included to being fully masked out.
Although it uses undocumented APIs, the code at http://spazzarama.wordpress.com/2009/02/12/screen-capture-with-vista-dwm/ will capture to a RGBA buffer which you can then use to render or save the image with the shadow and other translucency effects intact.
In DwmCapture.cs Change
BackBufferFormat = Format.X8R8G8B8
to
BackBufferFormat = Format.A8R8G8B8
(X8->A8)
And you should then be able to access both the usual RGB data plus transparency from the captured buffer. This can then be saved as a PNG or other format with alpha-channel for composing.
Removed idea that is terrible but would have been awesome back in the '90s
You say that using the DWM API only allows you to capture directly to the screen... could you create a window offscreen (say, X = -100000px, Y = -100000px) but visible (maybe even hidden?) and draw the screenshot to it? Since when using the DWM each window has a backing texture, I'm thinking it might still get drawn fine even though the target isn't directly onscreen.
Also, if you want to go the DirectX route and access the actual DX texture backing the window, I found a few leads that might help (especially the first link):
http://spazzarama.wordpress.com/2009/02/12/screen-capture-with-vista-dwm/
http://channel9.msdn.com/forums/TechOff/251261-Help-Getting-the-shared-window-texture-out-of-DWM-/
http://web.archive.org/web/20080625183653/http://www.aeroxp.org/board/index.php?showtopic=6286
http://www.youtube.com/watch?v=hRTgFTMnT_U
Using Graphics.CopyFromScreen or some other pinvoke variant of this:
This doesn't work because I can't
assume that the window I need
information from is at the top of the
z-order on the screen.
If you know which window you need the information from, can you bring it to the front, call Graphics.CopyFromScreen, and then reset its z-index? I know from experience that Aero does odd things when items are in the background in order to make their glass interface work correctly (partial rendering etc). This may not be great UX; however, it would be a special case and used only when Aero is turned on.
You can take a look at the source code of AeroShot, as described on the main page, it can capture rounded edges and with the Aero Glass transparency effect and save it to a PNG file. It's written in C#.

Categories