32bit rgb bmp to 24bit rgb bmp in C# problem - c#

I have to convert 32bit rgb bmp images in to 24 bits rgb bmp.
This is what i am trying to do
Bitmap b1=new Bitmap(sorecFileName);
Bitmap b2=new
Bitmap(b1.Size.Width,b1.Size.Height,System.Drawing.Imaging.PixelFormat.Format24bppRgb);
b2.SetResolution(b1.HorizontalResolution, b1.VerticalResolution);
Graphics g=Graphics.FromImage(b2);
g.DrawImage(b1,0,0);
//continue to draw on g here to add text or graphics.
g.Dispose();
b2.Save(destinationFileName);
The code compiles fine and generates the output image of 24bpp but its not in the rgb format any more. Why is this so?
I figured it out as I have a library that takes input of an image as rgb24 and displays it. So when I try to give the file generated by above code as input to the function, it displays noisy image.
However, if I open the same file in paint and save it as 24bpp bmp, and input it to the function, the picture displays fine. What am I missing?

b2.Save(destinationFileName);
You didn't specify the image file format. The default is PNG, not BMP. You now probably have a .bmp file on disk that actually contains a PNG image. That can go undetected for quite a while, lots of graphics programs pay attention to the file header instead of the file name extension. MSPaint for example will have no trouble loading the file. Just like any other program that uses GDI+. You might not be so lucky with a program that blindly assumes that the file contains a BMP and does no checking at all. Fix:
b2.Save(destinationFilename, System.Drawing.Imaging.ImageFormat.Bmp);

if you got a blackimage -
Add g.Clear(Color.White);

Related

Put .Gif Image from Clipboard to PictureBox

I want to copy gif image from Browser and paste to PictureBox
Image cImage = Clipboard.GetImage();
pictureBox1.Image = (Image)cImage;
This put image, but its not animated.
The image on the clipboard isn't a GIF, it's a Bitmap or DIB (or both). Those are the formats that you would see, if you used the old XP Clipboard Viewer (clipbrd.exe), or if you enumerated the available clipboard formats within your program.
See the list of standard clipboard formats: http://msdn.microsoft.com/en-us/library/windows/desktop/ff729168(v=vs.85).aspx
GIF is not one of them.
Also, there is no JPG or PNG. When you copy those images, they're placed onto the clipboard as CF_BITMAP and/or CF_DIB. Basically, Bitmap is the universal image format that everything converts to/from.
As an alternative, you MAY be able to get CF_HTML from the clipboard, figure out where the image is, and then fetch it from the server (or maybe the browser cache), preserving the original GIF information.

what will happen when 32BPP PNG is displayed in the old video driver (ony 256 colors)

I am wondering under the .Net, what if the 32BPP true colored PNG is displayed in the old display card? I believe there is no palette inside PNG file.
Anyone knows the internal logic behind this scenario?
Winforms calls RealizePalette() in the paint message handler to select the default Windows halftone palette, the one returned by Graphics.GetHalftonePalette(). The code it uses is very similar to the example shown in that MSDN article. The pixels in the bitmap are mapped to one of the 256 colors in the actual realized palette when it is drawn. The visual result is of course less than stellar, the 256 color mode is in the museum one stop past the floppy disk drive.

initializing a 48bpp bitmap from a file in c#

I write a 48bppRgb to a file but when I create a bitmap from this file it is 32bppArgb(object img2 has a property PixelFormat.32bppArgb).
Minimized example:
Bitmap img1 = new Bitmap(100, 100, PixelFormat.Format48bppRgb);
img1.Save("/img1.bmp");
Bitmap img2 = new Bitmap("/img1.bmp");
Why?
More than one problem. You didn't save the image in the BMP format. The default format for Image.Save(string) is PNG. The PNG encoder built into GDI+ doesn't support 48bpp images. Saving as a BMP requires specifying the image format:
Bitmap img1 = new Bitmap(100, 100, PixelFormat.Format48bppRgb);
img1.Save("c:/temp/img1.bmp", ImageFormat.Bmp);
You'll however find that the BMP encoder doesn't support 48bpp images either, you'll get a 24bpp image when you load it back. None of the codecs supports 48bpp.
There's lots of missing functionality in GDI+. ImageFormat.Icon doesn't work for example, it actually saves a PNG. And support for any of the Indexed pixels formats is quite poor. If you need this kind of support then you'll need a professional imaging library. LeadTools or ImageMagick are the usual choices.

How can I save an HTML string to a Bitmap \ Image?

I made a program that converts an image to ASCII based webpage.
http://postimage.org/image/2pi49sa4k/
That image was rendered using Chrome, on 50% size (miminal).
Obviously, I can make it myself because the code is only based on the FONT tag,
but I believe that it'll make things more complicated, as I'm not too good with graphics etc.
I would like to know how I can convert that string to a Bitmap or Image, with some parameters.(like size etc)
Thank you !

C# resize, resave .tif as .jpg gives a blue tint to the new image

I'm building a batch processor to save a directory of .tif images as .jpgs. The processing is working fine. However, the rendered jpgs have a blue-ish tint to them. They aren't "blue", as much as they have a cooler hue, a blue hue. The originals are much brighter and warmer in color. This is how I am creating the resized jpeg:
Bitmap bitmap = new Bitmap(image.Image, size);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.None;
graphics.DrawImage(image.Image, 0, 0, size.Width, size.Height);
// Get EncoderInfo("image/jpeg") gets the jpeg Codec by mime type
bitmap.Save(path, GetEncoderInfo("image/jpeg"), EncoderParameters);
The original tif images are 7MB is size - large in comparison to the rendered jpegs. Perhaps that has something to do with it. Not sure.
I've come up empty on the Googles. Does anyone have any experience with this or any advice on what to try next? Thanks!
It could be that the original files have a color profile. In that case, you need to copy that information into the new file as well. I don't know how to do that with .NET's imaging classes.
I would first suggest a test:
Create a plain image with a single colour.
Convert it to jpeg.
Then check in a photo package if the hue has actually changed or if it is your perception.
From the MSDN documentation, it looks like you can be doing this somewhat simpler (assuming image.Image is an actual System.Drawing.Image instance):
image.Image.Save(path, System.Drawing.ImageFormat.Jpeg)
That might help - it looks like you're taking your TIF, converting to a bitmap, and only then converting to a JPG.

Categories