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.
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.
I am using a memory stream to resize an image through a FileUpload control. After it resizes it I want it to save to my filesystem at "~/images/2012/" + filename.
How do I save the image from a memorystream?
System.Drawing.Image imageLarge = System.Drawing.Image.FromStream(stream);
System.Drawing.Image imageLarge1 = ResizeImage(imageLarge, 200, 300);
MemoryStream memolarge = new MemoryStream();
imageLarge1.Save(memolarge, System.Drawing.Imaging.ImageFormat.Jpeg);
System.Drawing.Image returnImage = System.Drawing.Image.FromStream(memolarge);
Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;
myEncoder = Encoder.Quality;
myEncoderParameter = new EncoderParameter(myEncoder, 100L);
myEncoderParameters.Param[0] = myEncoderParameter;
string convertedImage = returnImage.ToString();
returnImage.Save("~/images/2012/" + filename,
ImageFormat.Jpeg, myEncoderParameters);
This is the error I am getting along with an overloaded method error:
cannot convert from 'System.Drawing.Imaging.ImageFormat' to System.Drawing.Imaging.ImageCodecInfo
Look at the overloads of Image.Save.
Only the final two accept EncoderParameters, and neither of them accept an ImageFormat - both accept an ImageCodecInfo.
It's very important to be able to diagnose this kind of problem yourself:
Look at the compiler error carefully
Read the documentation
Check whether the call you're making makes sense
This has nothing to do with saving to a MemoryStream in particular - in fact, it's not clear why you're saving an Image and then immediately loading an Image from the same stream. (I'd advise setting the Position to 0 before you do so anyway, if you really want to keep doing this.)
Try this line instead:
returnImage.Save(
"~/images/2012/" + filename,
ImageCodecInfo.GetImageEncoders()
.Where(i => i.MimeType == "image/jpeg")
.First(),
myEncoderParameters);
See post Saving as jpeg from memorystream in c# it does something very similar to what you are looking for.
Also to change the codec properly
http://msdn.microsoft.com/en-us/library/ytz20d80.aspx
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.
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);