Bitmpa.Save & GZipStream - error

  • Thread starter Alexander Vasilevsky
  • Start date
A

Alexander Vasilevsky

Hi,

there is a code

using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.IO.Compression;
using System.Windows.Forms;

namespace TestImageTypeSize
{
class Program
{
static void Main()
{
ImageFormat[ ] formats = new[ ] { ImageFormat.Gif, ImageFormat.Jpeg,
ImageFormat.Png };

int screenId = 0;
foreach (Screen screen in Screen.AllScreens)
{
Bitmap bmp = new Bitmap(screen.Bounds.Width, screen.Bounds.Height);
Graphics g = Graphics.FromImage(bmp);

g.CopyFromScreen(screen.Bounds.X, screen.Bounds.Y, 0, 0, new
Size(screen.Bounds.Width, screen.Bounds.Height));

foreach (ImageFormat format in formats)
{
string filename = @"t:\screen_" + screenId + "." + format;

bmp.Save(filename, format);

FileStream fileStream = new FileStream(filename + ".zip",
FileMode.Create, FileAccess.ReadWrite);
GZipStream gZipStream = new GZipStream(fileStream,
CompressionMode.Compress);
bmp.Save(gZipStream, format);
gZipStream.Close();
}

screenId++;
}
}
}
}

which falls in line
bmp.Save(gZipStream, format);with exception
A generic error occurred in GDI+
What's error?


http://www.alvas.net - Audio tools for C# and VB.Net developers + Christmas
Gift
 
G

Göran Andersson

Alexander said:
Hi,

there is a code

8< snip
which falls in line
bmp.Save(gZipStream, format);with exception
A generic error occurred in GDI+
What's error?

Perhaps because you are using the same filename for all three files?

Why would you want to zip the files? The image formats are already
compressed, so you will not be able to get any significant compression.
Some files may even get larger if you zip them.
 
M

Manu

Hi,

there is a code

using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.IO.Compression;
using System.Windows.Forms;

namespace TestImageTypeSize
{
  class Program
  {
    static void Main()
    {
      ImageFormat[ ] formats = new[ ] { ImageFormat.Gif, ImageFormat.Jpeg,
ImageFormat.Png };

      int screenId = 0;
      foreach (Screen screen in Screen.AllScreens)
      {
        Bitmap bmp = new Bitmap(screen.Bounds.Width, screen.Bounds.Height);
        Graphics g = Graphics.FromImage(bmp);

        g.CopyFromScreen(screen.Bounds.X, screen.Bounds.Y, 0, 0, new
Size(screen.Bounds.Width, screen.Bounds.Height));

        foreach (ImageFormat format in formats)
        {
          string filename = @"t:\screen_" + screenId + "." + format;

          bmp.Save(filename, format);

          FileStream fileStream = new FileStream(filename + "..zip",
FileMode.Create, FileAccess.ReadWrite);
          GZipStream gZipStream = new GZipStream(fileStream,
CompressionMode.Compress);
          bmp.Save(gZipStream, format);
          gZipStream.Close();
        }

        screenId++;
      }
    }
  }

}

which falls in line
bmp.Save(gZipStream, format);with exception
A generic error occurred in GDI+
What's error?

http://www.alvas.net- Audio tools for C# and VB.Net developers + Christmas
Gift

I have checcked you code and it seems perfect to me.
I believe this is a bug in Framework.

The call to bmp.Save() succeeds only when Image Formats are Gif and
Jpeg. For all other formats it fails.
As a work around. you can always create a file from that Bitmap Object
and then compress it.

below is a code that will do if for you.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.IO.Compression;
using System.Windows.Forms;

namespace TestImageTypeSize
{
class Program
{
static void Main()
{
ImageFormat[] formats = new[] { ImageFormat.Gif,
ImageFormat.Jpeg,
ImageFormat.Png};

int screenId = 0;
foreach (Screen screen in Screen.AllScreens)
{
Bitmap bmp = new Bitmap(screen.Bounds.Width,
screen.Bounds.Height);
Graphics g = Graphics.FromImage(bmp);

g.CopyFromScreen(screen.Bounds.X, screen.Bounds.Y, 0,
0, new
Size(screen.Bounds.Width, screen.Bounds.Height));

g.Flush();

foreach (ImageFormat format in formats)
{
string filename = @"D:\screen_" + screenId + "." +
format;

bmp.Save(filename, format);

FileStream fileStream = new FileStream(filename +
".zip",
FileMode.Create, FileAccess.ReadWrite);
GZipStream gZipStream = new GZipStream(fileStream,
CompressionMode.Compress);

// My Code Starts Here

FileStream fs = new FileStream(filename,
FileMode.Open);
Byte[] bt = new byte[fs.Length];
fs.Read(bt,0,bt.Length);
fs.Close();

gZipStream.Write(bt, 0, bt.Length);
//My Code End here
gZipStream.Close();
fileStream.Close();
}

screenId++;
}
}
}

}
 
J

Jeff Johnson

ImageFormat[ ] formats = new[ ] { ImageFormat.Gif, ImageFormat.Jpeg,
ImageFormat.Png };

Is this syntax (using new without specifying the object) new to C# 3.0?
Because I like it! I've always thought you should be able to do this

MyClass c = new(param1, param2);

instead of the redundant

MyClass c = new MyClass(param1, param2);
 
J

Jeff Johnson

ImageFormat[ ] formats = new[ ] { ImageFormat.Gif, ImageFormat.Jpeg,
ImageFormat.Png };

Is this syntax (using new without specifying the object) new to C# 3.0?
Because I like it! I've always thought you should be able to do this

MyClass c = new(param1, param2);

instead of the redundant

MyClass c = new MyClass(param1, param2);

Aw, rats. Testing shows that the array syntax is allowed but you still have
to use the wordy syntax for classes. And yes, it's apparently new to C# 3.0,
although I don't find any references to it in the documentation for the new
operator. Hmmm, does this fall under the "Collection Initializers" topic?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top