Bitmap from Stream: Bug? - c#

I have a pretty strange Error. I need to scale down images, scale down the quality and convert to JPEG. This all works when I save the File on disk, but it doesn't work when I save it to a Stream.
System.Drawing.Bitmap bitmap = // valid Bitmap from Disk
System.IO.Stream stream = new MemoryStream();
// JPEG Encoding
System.Drawing.Imaging.ImageCodecInfo jpgEncoder = GetEncoder( System.Drawing.Imaging.ImageFormat.Jpeg );
System.Drawing.Imaging.Encoder encoder2 = System.Drawing.Imaging.Encoder.Quality;
System.Drawing.Imaging.EncoderParameters parameters = new System.Drawing.Imaging.EncoderParameters( 1 );
System.Drawing.Imaging.EncoderParameter parameter = new System.Drawing.Imaging.EncoderParameter( encoder2, qualityLevel );
parameters.Param[0] = parameter;
// Save downscaled on Disk and stream
bitmap.Save( stream, jpgEncoder, parameters );
bitmap.Save( #"C:\TestJPEG.jpg", jpgEncoder, parameters );
// some stream stuff
var bytes = ((MemoryStream)stream).ToArray();
System.IO.Stream inputStream = new MemoryStream( bytes );
// Load from disk and stream
Bitmap fromDisk = new Bitmap( #"C:\TestJPEG.jpg" ); // works
Bitmap fromStream = new Bitmap( inputStream ); // crash invalid parameter no inner message or description
Bitmap fromStream2 = (Bitmap)Bitmap.FromStream( inputStream ); // same error here
// also crashes if I load the "stream" named Stream
I can also open the converted file with Paint.
Any suggestions?
Edit:
I'm using .Net Framework 4.0 on Windows 7 Professional
Edit2:
Tried that Seek thing (Answer was deleted)
stream.Seek( 0, SeekOrigin.Begin );
It workes with the "old" stream. But i need to load it from a byte Array. Still same crash

The following code works for me:
var bitmap = new Bitmap(#"c:\Dokumente und Einstellungen\daniel.hilgarth\Desktop\Unbenannt.bmp");
ImageCodecInfo jpgEncoder = ImageCodecInfo.GetImageEncoders().Single(x => x.FormatDescription == "JPEG");
Encoder encoder2 = System.Drawing.Imaging.Encoder.Quality;
EncoderParameters parameters = new System.Drawing.Imaging.EncoderParameters( 1 );
EncoderParameter parameter = new EncoderParameter( encoder2, 50L );
parameters.Param[0] = parameter;
System.IO.Stream stream = new MemoryStream();
bitmap.Save( stream, jpgEncoder, parameters );
bitmap.Save(#"C:\Temp\TestJPEG.jpg", jpgEncoder, parameters);
var bytes = ((MemoryStream)stream).ToArray();
System.IO.Stream inputStream = new MemoryStream(bytes);
Bitmap fromDisk = new Bitmap(#"C:\Temp\TestJPEG.jpg");
Bitmap fromStream = new Bitmap(inputStream);
There are a few differences to your code. Which one causes your problem is up to you to find out, I guess:
I used 50L as qualityLevel. When using 1, 2, 50 or 100, I was getting an ArgumentException "Parameter is not valid". As I don't know the type or value of your qualityLevel variable that can very well be the problem.
I replaced your GetEncoder method. I don't know what your method does exactly, so it could be the problem, but I doubt it.

Related

how to convert an image into base64 in xamarin.android?

I have this code, it works very well in android studio but not in xamarin
bitmap.Compress() has different arguments in xamarin and i am confused how to convert image into base64 or binary in xamarin.android?
I am receving an error in the 3rd line:
( bitmap.Compress() has some invalid arguments).
Bitmap bitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.ace1);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100,bao);
byte[] ba = bao.ToByteArray();
string bal = Base64.EncodeToString(ba, Base64.Default);
If you look at the documentation for Bitmap.Compress in Xamarin, you'll see that the last parameter is a Stream.
The equivalent of ByteArrayOutputStream in .NET is MemoryStream, so your code would be:
Bitmap bitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.ace1);
MemoryStream stream = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, stream);
byte[] ba = stream.ToArray();
string bal = Base64.EncodeToString(ba, Base64Flags.Default);
(You could use Convert.ToBase64String instead of Base64.EncodeToString if you wanted, too.)
This is how I'm getting a Byte[] for my Bitmap object:
Byte[] imageArray = null;
Bitmap selectedProfilePic = this.GetProfilePicBitmap ();
if (selectedProfilePic != null) {
using (var ms = new System.IO.MemoryStream ()) {
selectedProfilePic.Compress (Bitmap.CompressFormat.Png, 0, ms);
imageArray = ms.ToArray ();
}
}
Hope this helps.

Compress image without saving it

I am using the following code to compress an image and it does a nice job but I want to use the compressed image not save it. So right now I have to save the image then read it in again which is slow. Is there a way of compressing it with out saving it.
private void compress(System.Drawing.Image img, long quality, ImageCodecInfo codec)
{
EncoderParameters parameters = new EncoderParameters(1);
parameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);
img.Save("check1.jpg", codec, parameters);
}
private static ImageCodecInfo GetCodecInfo(string mimeType)
{
foreach (ImageCodecInfo encoder in ImageCodecInfo.GetImageEncoders())
if (encoder.MimeType == mimeType)
return encoder;
throw new ArgumentOutOfRangeException(
string.Format("'{0}' not supported", mimeType));
}
There is an overload that takes a Stream so you can save it straight to a MemoryStream and won't need to save to disk/reload.
EncoderParameters parameters = new EncoderParameters(1);
parameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);
var ms = new MemoryStream();
img.Save(ms, codec, parameters);
//Do whatever you need to do with the image
//e.g.
img = Image.FromStream(ms);
The reason you're getting the "Parameter not valid" exception you mention in the comments is because the image isn't being disposed of before you try to call FromStream, so you'll need to dispose it. Also, I don't know how you're calling this method, but you should probably update it to return the MemoryStream.
private MemoryStream compress(System.Drawing.Image img, long quality, ImageCodecInfo codec)
{
EncoderParameters parameters = new EncoderParameters(1);
parameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);
var ms = new MemoryStream();
img.Save(ms, codec, parameters);
return ms;
}
public void MyMethod()
{
MemoryStream ms;
using(var img = Image.FromFile("myfilepath.img"))
{
ms = compress(img, /*quality*/, /*codec*/);
}
using(var compressedImage = Image.FromStream(ms))
{
//Use compressedImage
}
}
Notice how I return ms from compress and capture it. Also, more importantly, how we wrap the initial img in a using statement which will dispose the file handle correctly, and after that gets disposed create the second compressedImage which is also in a using so it will also get disposed of properly when you're done.

Image compression is not working

I have an operation on the site that takes crops an image, however the resultant, cropped image is coming out significantly larger in terms of file size (original is 24k and the cropped image is like 650k). So I found that I need to apply some compression to the image before saving it. I came up with the following:
public static System.Drawing.Image CropImage(System.Drawing.Image image, Rectangle cropRectangle, ImageFormat format)
{
var croppedImage = new Bitmap(cropRectangle.Width, cropRectangle.Height);
using (var g = Graphics.FromImage(croppedImage))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(
image,
new Rectangle(new Point(0,0), new Size(cropRectangle.Width, cropRectangle.Height)),
cropRectangle,
GraphicsUnit.Pixel);
return CompressImage(croppedImage, format);
}
}
public static System.Drawing.Image CompressImage(System.Drawing.Image image, ImageFormat imageFormat)
{
var bmp = new Bitmap(image);
var codecInfo = EncoderFactory.GetEncoderInfo(imageFormat);
var encoder = System.Drawing.Imaging.Encoder.Quality;
var parameters = new EncoderParameters(1);
var parameter = new EncoderParameter(encoder, 10L);
parameters.Param[0] = parameter;
using (var ms = new MemoryStream())
{
bmp.Save(ms, codecInfo, parameters);
var resultImage = System.Drawing.Image.FromStream(ms);
return resultImage;
}
}
I set the quality low just to see if there was any change at all. There isn't. The crop is being saved correctly appearance-wise but compression is a no joy. If I bypass CompressImage() altogether, neither the file size nor the image quality appear to be any different.
So, 2 questions. Why is nothing happening? Is there a simpler way to compress the resultant image to "web-optimize" similar to how photoshop saves web images (I thought it just stripped a lot of info out of it to reduce the size).
Your problem is you must 'compress' (really encode) the image as you save it, not before you save it. An Image object in your program is always uncompressed.
By saving to the MemoryStream and reading back out from the stream will encode the image and then decode it back to the same size again (with some quality loss in the process if you are using JPEG). However, if you save it to a file with the compression parameters, you will get a compressed image file.
Using this routine with JPEG quality level 90 on a 153 KB source image gives an output image of 102 KB. If you want a smaller file size (with more encoding artifacts) change the encoder parameter to something smaller than 90.
public static void SaveJpegImage(System.Drawing.Image image, string fileName)
{
ImageCodecInfo codecInfo = ImageCodecInfo.GetImageEncoders()
.Where(r => r.CodecName.ToUpperInvariant().Contains("JPEG"))
.Select(r => r).FirstOrDefault();
var encoder = System.Drawing.Imaging.Encoder.Quality;
var parameters = new EncoderParameters(1);
var parameter = new EncoderParameter(encoder, 90L);
parameters.Param[0] = parameter;
using (FileStream fs = new FileStream(fileName, FileMode.Create))
{
image.Save(fs, codecInfo, parameters);
}
}
I believe you shouldn't dispose of the MemoryStream while you are using an image created using Image.FromStream that refers to the stream. Creating a Bitmap directly from the stream also doesn't work.
Try this:
private static Image CropAndCompressImage(Image image, Rectangle rectangle, ImageFormat imageFormat)
{
using(Bitmap bitmap = new Bitmap(image))
{
using(Bitmap cropped = bitmap.Clone(rectangle, bitmap.PixelFormat))
{
using (MemoryStream memoryStream = new MemoryStream())
{
cropped.Save(memoryStream, imageFormat);
return new Bitmap(Image.FromStream(memoryStream));
}
}
}
}

C# Image.Save ends in "Invalid Bmp"

I try to create a memory stream of an object which is indeed a System.Drawing.Bitmap. The MemoryStream is later on saved on the disk as a temporary file.
This is the code:
object resource = TestData.ResourceManager.GetObject(key);
...
if (resource is System.Drawing.Bitmap)
{
MemoryStream memoryStream = new MemoryStream();
Encoder myEncoder = System.Drawing.Imaging.Encoder.ColorDepth;
EncoderParameters myEncoderParameters = new EncoderParameters();
myEncoderParameters.Param[0] = new EncoderParameter(myEncoder, 16);
((System.Drawing.Bitmap)resource).Save(memoryStream, ImageFormat.Bmp);
return memoryStream;
}
...
The memory stream is then saved:
string targetFilePath = Path.GetTempFileName();
using (MemoryStream resourceStream = ResolveAsStream(key))
{
if (resourceStream == null)
{
throw new InvalidOperationException("Could not find resource for key '" + key + "'.");
}
using (FileStream fs = new FileStream(targetFilePath, FileMode.Open, FileAccess.Write))
{
resourceStream.CopyTo(fs);
}
}
return targetFilePath;
The file is successfully created but when I try to open it, it tells me, that the image file is invalid (i.e. it cannot be opened by Paint or any other image viewer). Instead of using ImageFormat.Bmp I tried with ImageFormat.MemoryBmp:
object resource = TestData.ResourceManager.GetObject(key);
...
if (resource is System.Drawing.Bitmap)
{
MemoryStream memoryStream = new MemoryStream();
Encoder myEncoder = System.Drawing.Imaging.Encoder.ColorDepth;
EncoderParameters myEncoderParameters = new EncoderParameters();
myEncoderParameters.Param[0] = new EncoderParameter(myEncoder, 16);
((System.Drawing.Bitmap)resource).Save(memoryStream, ImageFormat.MemoryBmp);
return memoryStream;
}
...
But this actually fails with
"value Cannot be null Parameter name: encoder"
It seems there is no codec specified for MemoryBmp. So how can I save the image correctly so that the Bmp file is valid?

Compress bitmap before sending over network

I'm trying to send a bitmap screenshot over network, so I need to compress it before sending it. Is there a library or method for doing this?
When you save an Image to a stream, you have to select a format. Almost all bitmap formats (bmp, gif, jpg, png) use 1 or more forms of compression. So just select an appropriate format, and make make sure that sender and receiver agree on it.
If you are looking for something to compress the image in quality, here it is-
private Image GetCompressedBitmap(Bitmap bmp, long quality)
{
using (var mss = new MemoryStream())
{
EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
ImageCodecInfo imageCodec = ImageCodecInfo.GetImageEncoders().FirstOrDefault(o => o.FormatID == ImageFormat.Jpeg.Guid);
EncoderParameters parameters = new EncoderParameters(1);
parameters.Param[0] = qualityParam;
bmp.Save(mss, imageCodec, parameters);
return Image.FromStream(mss);
}
}
Use it -
var compressedBmp = GetCompressedBitmap(myBmp, 60L);
Try the System.IO.DeflateStream class.
May be you can use:
private Bitmap compressImage(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//
int options = 100;
while ( baos.toByteArray().length / 1024>100) { //
baos.reset();
image.compress(Bitmap.CompressFormat.JPEG, options, baos);//
options -= 10;// 10
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//
return bitmap;
}

Categories