Generating Text to a PNG File with Transparent Background - c#

I need to Export some PNG Images with Transparent Background from a C# Application .
But that is not a Huge Concern .
What make's it complicated and beyond my knowledge is ,how am i able to Export to PNG Image File With Transparent Background with Some Text in it,like a Label only without Background so in that way i can export as many images i want with Different Text into it.
And that PNG should had the Size of the Label ,or if there is a way it should FIT the Font Size and Text Length ,so it Height and Width should be same as Font one .
Bests.

You're looking for the Bitmap and Graphics classes, along with the Font class and the TextRenderer.MeasureText method.

Related

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

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?

How can I set a transparent image to my form's background in c#?

I made a transparent background image in photoshop but when I use it as my form's background image,it doesn't use form's back color(blue),so that I cannot use this code to make a transparent form.
this.BackColor = Color.blue;
this.TransparencyKey = Color.blue;
If I use white color instead,my other tools fore color face problem.I'm trying to make windows form app.I don't know if there are easier & better alternatives to make a transparent form.
What should I do?
Referencing a post from here and my own experience with having trouble on form transparency with types of images I expect it's because it's the fact it's JPG and not PNG or some other transparent friendly image format. I had the same problem and when I changed to PNG format it worked for me.
Reading results from a couple of random google searches it seems JPEG doesn't support transparency do to file format limitations.
Why am I getting a black background around my images when resizing even when Bitmap is set to Graphics.Clear(Color.Transparent)

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.

Is there any way to have a background image in MigraDoc / PDFsharp?

I want to create a cover page that has text over a background image. Is this possible in MigraDoc / PDFsharp?
Yes.
Background image can be a page from a PDF file or a "real" image (JPEG, BMP, ...).
Add the background image first at an absolute position. Following text will be rendered above the image.
See members Left, Top, RelativeVertical, RelativeHorizontal, etc.
See also here (Images and TextFrames are both Shapes):
http://forum.pdfsharp.net/viewtopic.php?p=3133#p3133

I am trying to save images in wpf application, but after saving some pink color is coming on that images. Is there any solution for that?

I am developing a WPF application. In which I am trying to save images. After saving the image, the color balance of the image turns to light reddish. How can we retain the original color of image after saving also. Is there any solution for that.
I suspect you are saving images with transparency into a BITMAP format. Bitmaps do not support transparency as such, but usually use a colour replacement technology to give the illusion of transparency. The default colour for this is magenta I believe.
You might try saving the images either as PNG's, or perhaps there is a way to specify the transparency colour (I'm not sure).
It sounds like this could be because your source image is in CMYK colour mode and your output is in RGB colour mode.
Could you please provide us with some more information about the source image and output image? (colour mode, image format, etc...)
Also, what sort of image processing are you doing? (resizing, saving a bitmap as a jpeg, etc...)

Categories