|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Bitmpa.Save & GZipStream - errorthere 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 Alexander Vasilevsky wrote:
> Hi, 8< snip> > there is a code > > Perhaps because you are using the same filename for all three files?> which falls in line > bmp.Save(gZipStream, format);with exception > A generic error occurred in GDI+ > What's error? 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.
Show quote
Hide quote
On Dec 13, 7:47 pm, "Alexander Vasilevsky" <m***@alvas.net> wrote: I have checcked you code and it seems perfect to me.> 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 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++; } } } } "Alexander Vasilevsky" <m***@alvas.net> wrote in message Is this syntax (using new without specifying the object) new to C# 3.0? news:gi0hmi$1kas$1@behemoth.volia.net... > ImageFormat[ ] formats = new[ ] { ImageFormat.Gif, ImageFormat.Jpeg, > ImageFormat.Png }; 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);
Show quote
Hide quote
"Jeff Johnson" <i.get@enough.spam> wrote in message Aw, rats. Testing shows that the array syntax is allowed but you still have 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); 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?
Other interesting topics
Array - Does it exist?
How to close a StreamWriter class when the program exits Compare Values Dllimport System AccessViolationExecption UserControl, GUI inside the application on runing time, PlugIn problem linq --> changing a value? Filewatcher - what happens if a file is created while processing another one? what does this statement double? pPlanMinimum; mean? validating xml file methods Connection string/Sql Server 2005/Windows authentication/ but not on domain |
|||||||||||||||||||||||