How do I get C# to force bitmap images that are saved to be saved as 24-bit images as can be seen when you get the right-click properties of the image in Windows. All the images I save are set to 32-bit.
I tried the below code with no luck. The source images are all 24-bit as well but are always saved as 32-bit images.
ImageCodecInfo bmpCodec = FindEncoder(ImageFormat.Bmp);
EncoderParameters parameters = new EncoderParameters();
parameters.Param[0] = new EncoderParameter(Encoder.ColorDepth, 24);
imgCheque.Save(DestinationFile.ToString(), bmpCodec, parameters);
The images have to be properly 24 bit as the are read by a different program that can't handle 32-bit images.
Thanks in advance,
Soultech
Is this any use?
// imgCheque source created somewhere else up here
using (Bitmap blankImage = new Bitmap(imgCheque.Width, imgCheque.Height, PixelFormat.Format24bppRgb))
{
using (Graphics g = Graphics.FromImage(blankImage))
{
g.DrawImageUnscaledAndClipped(imgCheque, new Rectangle(Point.Empty, imgCheque.Size));
}
ImageCodecInfo bmpCodec = FindEncoder(ImageFormat.Bmp);
blankImage.Save(#"C:\TEMP\output.bmp", bmpCodec, null);
}
Try this?
ImageCodecInfo bmpCodec = FindEncoder(ImageFormat.Bmp);
EncoderParameters parameters = new EncoderParameters();
parameters.Param[0] = new EncoderParameter(Encoder.ColorDepth, 24L);
imgCheque.Save(DestinationFile.ToString(), bmpCodec, parameters);
Related
I've been trying to find a ways to write in information about jpeg colour space information, since regular saving does not do that, and barely anyone talks about it. Part of the code I use to save into jpeg:
using (Bitmap bmp = new Bitmap(bitmapPicture))
{
ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 100L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp.Save(textBox1.Text + "\\" + textBox2.Text + ".jpg", jpgEncoder, myEncoderParameters);
}
It does the trick, but it doesn't add colour space information which I think is one of the problems when opening that picture with different applications. The colours appears to be incorrect. Also for some reason bitmap brush only uses argb colour space (notice code below). If I'd type in '.FromSrgb' or '.FromRgb' it errors it out (apparently no such thing exists). This is example how I create bitmap which later on is being saved:
using (Graphics gfx = Graphics.FromImage(bitmapPicture))
using (SolidBrush brush = new SolidBrush(System.Drawing.Color.FromArgb(255, 255, 255)))
{
gfx.FillRectangle(brush, 0, 0, with, height);
gfx.DrawImage(addOtherImageOnSameBitmap, xCoordinate, yCoordinate, with2, height2);
}
Also tried doing that with Save File Dialog (code below), but the result is the same - jpeg colours is way off and has no information about colour space.
SaveFileDialog save = new SaveFileDialog();
save.Filter = "Images|*.jpg";
ImageFormat format = ImageFormat.Png;
if (save.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string ext = System.IO.Path.GetExtension(save.FileName);
switch (ext)
{
case ".jpg":
format = ImageFormat.Jpeg;
break;
}
pictureBox1.Image.Save(save.FileName, format);
}
The problem is fixed when opening that image in photoshop and saving it with sRGB colour space. Is it possible to do that in C#? To save jpeg in sRGB and adding this information to jpeg file itself? Because going into photoshop to fix this problem seems to be a bit too much of a hassle (especially when dealing with a lot of pictures).
P.S. sorry if posted codes seems somewhat off, I just started learning C# couple days ago.
Here's the deal. I'm trying to use a function to compress an image from my notebook camera while using a standard code to do that:
public static byte[] EncodeImage(this Image image)
{
using (MemoryStream ms = new MemoryStream())
{
ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");
EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 80);
EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = qualityParam;
image.Save(ms, jpegCodec, encoderParams);
return ms.ToArray();
}
}
After the compression is done, I save it to the database. Works good, providing me with a good amount of compression (500 000 bytes vs 32 000). Some time later, I need to show the image to the user in a winforms application.
using (MemoryStream ms = new MemoryStream(entity.Photo))
fotoPictureBox.Image = Image.FromStream(ms);
Again, this displays a nice image. However, then I want the user to be able to download the image and save where he sees suitable. And this is when all the problems arise.
If I use this code:
fotoPictureBox.Image.Save(imageSaveFileDialog.FileName);
I receieve a typical generic GDI+ error.
If I try to save the image by using the code I've encoded it
public static void SaveEncodedImage(this Image image, string targetPath)
{
ImageCodecInfo jpegCodec = Pomocne.GetEncoderInfo("image/jpeg");
EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 80);
EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = qualityParam;
image.Save(targetPath, jpegCodec, encoderParams);
}
I get this error (pointing to the line image.Save(targetPath, jpegCodec, encoderParams);):
An unhandled exception of type System.ArgumentException occurred in System.Drawing.dll
Additional information: Parameter is not valid.
Can please anyone help? It's rather urgent, thank you.
To address some questions/answers you will probably start with - if I don't compress the image with this code, I can save the image to the chosen path without any problems. So there's clearly some problem with decoding or something like that. Don't ask me if I have sufficient permissions, because I have.
For some reason this code works great:
File.WriteAllBytes(targetPath, byteArray);
I guess sometimes the easiest solution is the right one. Not sure about the exact explanation (you can provide one, I'll surely vote up), but this is resolved.
Thanks anyway.
I'm looking for a way to take in a 32 bit bitmap and save it again however deinterlacing the frames. When the image is taken two fields are visible but only the last one is necessary. Is this possible to do using EncoderParameters. This is what I've tried so far:
using (Image source = Image.FromFile(#"C:\Users\Martin vanPutten\Desktop\test.bmp"))
{
ImageCodecInfo codec = ImageCodecInfo.GetImageEncoders().First(c => c.MimeType == "image/bmp");
EncoderParameters parameters = new EncoderParameters(3);
parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
parameters.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.ScanMethod, (int)EncoderValue.LastFrame);
parameters.Param[2] = new EncoderParameter(System.Drawing.Imaging.Encoder.RenderMethod, (int)EncoderValue.RenderNonProgressive);
source.Save(#"C:\Users\Martin vanPutten\Desktop\test2.bmp", codec, parameters);
}
Is there another way to do this? All I need to do is remove the second overlapping frame in an image.
Quick update, its not that it has two frames, but 2 fields in 1 frame.
So I've come across a rather odd situation.
I'm using the following to Save a PNG (lossless) image,
public static void SaveJpeg(string path, Image image, int quality)
{
if ((quality < 0) || (quality > 100))
{
string error = string.Format("Jpeg image quality must be between 0 and 100, with 100 being the highest quality. A value of {0} was specified.", quality);
throw new ArgumentOutOfRangeException(error);
}
EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
ImageCodecInfo imgCodec = GetEncoderInfo("image/png");
EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = qualityParam;
image.Save(path, imgCodec, encoderParams);
}
public static ImageCodecInfo GetEncoderInfo(string mimeType)
{
string lookupKey = mimeType.ToLower();
ImageCodecInfo foundCodec = null;
if (Encoders.ContainsKey(lookupKey))
{
foundCodec = Encoders[lookupKey];
}
return foundCodec;
}
This code works great in XP, the image gets saved lossless, when I zoom in, I see no pixelation whatsoever however, when this same compiled application is ran on a windows7 machine, the saved image looks pixelated.
Is this due to the way I'm saving the image or perhaps something changing with the image save functionality / encoding in windows 7?
Your Win7 image is anti-aliased.
This is good, not bad; it makes the text smoother.
It's controlled by properties in the Graphics class.
I'm attempting to resize and image using the following function and receiving this error:
Exception Details: System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.
on this line:
imageConvertedToBitmap.Save(cachedFileName, info[1], encoderParameters);
Any ideas why this might be happening?
private byte[] GetCachedImage(string cachedFileName, string pathToImage, int width, int height)
{
if (!System.IO.File.Exists(cachedFileName) || (System.IO.File.GetLastWriteTime(pathToImage) > System.IO.File.GetLastWriteTime(cachedFileName)))
{
Image imageToResize = Image.FromFile(pathToImage);
Image imageConvertedToBitmap = new Bitmap(width, height);
Graphics graphicsController = Graphics.FromImage(imageConvertedToBitmap);
graphicsController.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphicsController.SmoothingMode = SmoothingMode.HighQuality;
graphicsController.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphicsController.CompositingQuality = CompositingQuality.HighQuality;
graphicsController.DrawImage(imageToResize, 0, 0, width, height);
EncoderParameters parameters = new EncoderParameters(1);
parameters.Param[0] = new EncoderParameter(Encoder.Quality, 80L);
ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders();
EncoderParameters encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 80L);
MemoryStream memoryStream = new MemoryStream();
imageConvertedToBitmap.Save(cachedFileName, info[1], encoderParameters);
imageConvertedToBitmap.Save(memoryStream, info[1], encoderParameters);
imageToResize.Dispose();
imageConvertedToBitmap.Dispose();
graphicsController.Dispose();
parameters.Dispose();
encoderParameters.Dispose();
return memoryStream.GetBuffer();
}
byte[] buffer = null;
try
{
FileStream fileStream = new FileStream(cachedFileName, FileMode.Open, FileAccess.Read);
BinaryReader binaryReader = new BinaryReader(fileStream);
long totalBytes = new FileInfo(cachedFileName).Length;
buffer = binaryReader.ReadBytes((Int32)totalBytes);
fileStream.Close();
fileStream.Dispose();
binaryReader.Close();
}
catch { }
return buffer;
}
It was a permissions error. Had to give appropriate Write permission to the directory I was saving to. Sometimes it's the simple things. :)
From what I read, when getting this GDI+ error, the first thing to check should be permissions as it's almost always an indicator of a security problem.
Try an overload of Save that doesn't take EncoderParameters as a parameter. It should work fine and produce the resized image that you're looking for.
I had this error one time, it was because I tried to save under a locked file by a Bitmap object.
We just see your method but not how you use it.
I think there are several syntax problems in your code : you must use "using() { }" for all the disable objects in your code.
using(Image imageToResize = Image.FromFile(pathToImage))
using(Image imageConvertedToBitmap = new Bitmap(width, height))
etc.
{
}
Your got this error on another instance.
Firstly understand what this error means the other way:
When you get the error, go to task manager and navigate to Details tab.
Right click on the table headers and click select columns.
Tick the GDI objects checkbox.
Now find the name of your program in the table and check the no. of GDI objects.
It would have exceeded 10000 or must be at 10000.
The error pops up whenever the no. of GDI objects by a program exceeds
10000 as 10000 is the limit.
Check how many times the System.Drawing.... runs in your code.
Prevent objects like Fonts or Images from being created again and again.
Instead set a reference point.