Home All Groups Group Topic Archive Search About

Bitmpa.Save & GZipStream - error

Author
13 Dec 2008 2:47 PM
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

Author
13 Dec 2008 3:32 PM
Göran_Andersson
Alexander Vasilevsky wrote:
> 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.

--
Göran Andersson
_____
http://www.guffa.com
Are all your drivers up to date? click for free checkup

Author
14 Dec 2008 6:55 PM
Manu
Show quote Hide quote
On Dec 13, 7:47 pm, "Alexander Vasilevsky" <m***@alvas.net> wrote:
> 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++;
            }
        }
    }

}
Author
15 Dec 2008 2:46 PM
Jeff Johnson
"Alexander Vasilevsky" <m***@alvas.net> wrote in message
news:gi0hmi$1kas$1@behemoth.volia.net...

>      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);
Author
15 Dec 2008 8:19 PM
Jeff Johnson
Show quote Hide quote
"Jeff Johnson" <i.get@enough.spam> wrote in message
news:urxWaPsXJHA.5064@TK2MSFTNGP02.phx.gbl...

>>      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?

Bookmark and Share