From 150ea9c4eb2308a0c0c35dc76b15203aae6cc20c Mon Sep 17 00:00:00 2001 From: RKrom Date: Tue, 21 Feb 2012 13:30:43 +0000 Subject: [PATCH] Fixed a problem with writing transparent bitmaps to non transparent image formats. Cleaned up some code, implemented the start of a donate page in the installer (also tweaked the size a bit) git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@1673 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4 --- Greenshot/Helpers/ImageOutput.cs | 59 ++++++----- Greenshot/Helpers/PrintHelper.cs | 23 ++-- .../releases/additional_files/donate.bmp | Bin 0 -> 11150 bytes .../releases/additional_files/license.txt | 6 +- Greenshot/releases/build.bat | 1 - Greenshot/releases/innosetup/setup.iss | 98 +++++++++++++++++- GreenshotPlugin/Core/ImageHelper.cs | 21 ++++ 7 files changed, 157 insertions(+), 51 deletions(-) create mode 100644 Greenshot/releases/additional_files/donate.bmp diff --git a/Greenshot/Helpers/ImageOutput.cs b/Greenshot/Helpers/ImageOutput.cs index cd7a9699b..8fd289cfa 100644 --- a/Greenshot/Helpers/ImageOutput.cs +++ b/Greenshot/Helpers/ImageOutput.cs @@ -76,41 +76,41 @@ namespace Greenshot.Helpers { /// Saves image to stream with specified quality /// public static void SaveToStream(Image imageToSave, Stream stream, OutputFormat extension, int quality) { - ImageFormat imfo = null; - bool disposeImage = false; + ImageFormat imageFormat = null; + //bool disposeImage = false; switch (extension) { case OutputFormat.bmp: - imfo = ImageFormat.Bmp; + imageFormat = ImageFormat.Bmp; break; case OutputFormat.gif: - imfo = ImageFormat.Gif; + imageFormat = ImageFormat.Gif; break; case OutputFormat.jpg: - imfo = ImageFormat.Jpeg; + imageFormat = ImageFormat.Jpeg; break; case OutputFormat.png: - imfo = ImageFormat.Png; + imageFormat = ImageFormat.Png; break; case OutputFormat.tiff: - imfo = ImageFormat.Tiff; + imageFormat = ImageFormat.Tiff; break; default: - imfo = ImageFormat.Png; + imageFormat = ImageFormat.Png; break; } - // If Quantizing is enable, overwrite the image to save with a 256 - color version - if (conf.OutputFileReduceColors) { - try { - LOG.Debug("Reducing colors on bitmap."); - Quantizer quantizer = new OctreeQuantizer(255,8); - imageToSave = quantizer.Quantize(imageToSave); - // Make sure the "new" image is disposed - disposeImage = true; - } catch(Exception e) { - LOG.Warn("Error occurred while Quantizing the image, ignoring and using original. Error: ", e); - } - } + //// If Quantizing is enable, overwrite the image to save with a 256 - color version + //if (conf.OutputFileReduceColors) { + // try { + // LOG.Debug("Reducing colors on bitmap."); + // Quantizer quantizer = new OctreeQuantizer(255,8); + // imageToSave = quantizer.Quantize(imageToSave); + // // Make sure the "new" image is disposed + // disposeImage = true; + // } catch(Exception e) { + // LOG.Warn("Error occurred while Quantizing the image, ignoring and using original. Error: ", e); + // } + //} try { // Create meta-data @@ -119,23 +119,28 @@ namespace Greenshot.Helpers { try { imageToSave.SetPropertyItem(softwareUsedPropertyItem); } catch (ArgumentException) { - LOG.WarnFormat("Image of type {0} do not support property {1}", imfo, softwareUsedPropertyItem.Id); + LOG.WarnFormat("Image of type {0} do not support property {1}", imageFormat, softwareUsedPropertyItem.Id); } } LOG.DebugFormat("Saving image to stream with PixelFormat {0}", imageToSave.PixelFormat); - if (imfo == ImageFormat.Jpeg) { + if (imageFormat == ImageFormat.Jpeg) { EncoderParameters parameters = new EncoderParameters(1); parameters.Param[0] = new System.Drawing.Imaging.EncoderParameter(Encoder.Quality, quality); ImageCodecInfo[] ies = ImageCodecInfo.GetImageEncoders(); imageToSave.Save(stream, ies[1], parameters); + } else if (imageFormat != ImageFormat.Png && Image.IsAlphaPixelFormat(imageToSave.PixelFormat)) { + // No transparency in target format + using (Bitmap tmpBitmap = ImageHelper.Clone(imageToSave, PixelFormat.Format24bppRgb)) { + tmpBitmap.Save(stream, imageFormat); + } } else { - imageToSave.Save(stream, imfo); + imageToSave.Save(stream, imageFormat); } } finally { - // cleanup if needed - if (disposeImage && imageToSave != null) { - imageToSave.Dispose(); - } + // // cleanup if needed + // if (disposeImage && imageToSave != null) { + // imageToSave.Dispose(); + // } } } diff --git a/Greenshot/Helpers/PrintHelper.cs b/Greenshot/Helpers/PrintHelper.cs index 253698cf7..26b1f6522 100644 --- a/Greenshot/Helpers/PrintHelper.cs +++ b/Greenshot/Helpers/PrintHelper.cs @@ -142,20 +142,6 @@ namespace Greenshot.Helpers { } } - // Invert Bitmap if wanted - if (conf.OutputPrintInverted) { - using (BitmapBuffer bb = new BitmapBuffer((Bitmap)image, false)) { - bb.Lock(); - for (int y = 0; y < bb.Height; y++) { - for (int x = 0; x < bb.Width; x++) { - Color color = bb.GetColorAt(x, y); - Color invertedColor = Color.FromArgb(color.A, color.R ^ 255, color.G ^ 255, color.B ^ 255); - bb.SetColorAt(x, y, invertedColor); - } - } - } - } - // Get a rectangle representing the printable Area RectangleF pageRect = e.PageSettings.PrintableArea; @@ -192,8 +178,13 @@ namespace Greenshot.Helpers { e.Graphics.DrawString(dateString, f, Brushes.Black, pageRect.Width / 2 - (dateStringWidth / 2), pageRect.Height); } } - - e.Graphics.DrawImage(image, printRect, imageRect, GraphicsUnit.Pixel); + if (conf.OutputPrintInverted) { + using (Bitmap negativeBitmap = ImageHelper.CreateNegative((Bitmap)image)) { + e.Graphics.DrawImage(negativeBitmap, printRect, imageRect, GraphicsUnit.Pixel); + } + } else { + e.Graphics.DrawImage(image, printRect, imageRect, GraphicsUnit.Pixel); + } } } } diff --git a/Greenshot/releases/additional_files/donate.bmp b/Greenshot/releases/additional_files/donate.bmp new file mode 100644 index 0000000000000000000000000000000000000000..26e4697fc1cc2174889575103ed6aacf6cec0358 GIT binary patch literal 11150 zcmchd2dq`q5{56mccdi(0>YDG5TzM927(lU@KBHvgD3$DAXP)}NC!bF4<4Zt6r@N8 z31C17C@5ASfb?z!l=nW)?-}Rpea_{2Z{9n}-I+aW)~tW6HEY()?AxIEqzXxqUp1cp z;I9gQP54WfRD`E~yK^Tc-TRCF{^Ndk@7}$0=gychW43JB0{f3Y{!ruDvuEFY^UeMH z_dD4b^7{4bBSwt)`s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f- zj(#F%M6`u~Pr%|1WlY76f>xtO4daK2^5x6>^G=r#E+y}A_lWSFXwzr$;SoFshb&M$ z*;C&XrY*ixmsng}Rm98TPU3J$aD_qpD8-OGLkg4Y176g0TGMKN7Df{ zrAn1LdGe%!`?#wakmq&6gb6m(z<~q90-l3K`^(hf!dscS`yQg3Hf`$j{jflogKsii zO4`FR;%K5iH*&a0^pHxl7Np{jKKf`P#c}y0Mpv#}5j3*&xH4jD&?Zfq*gPDrnl)=C z-xae;p=Ft#ci(-Np*C&Wl#-6*Nq7`Ni{c`@CT%o*afw7r8CO_L>IP)sDfD-AVFt0d zm{>CE|85grlB z2A9p8y{2?Q7eY{~5oODkr56Q>9zA+cbd)MR2?X)s+y*Kqn+5?ORYNR_E8zE@c;X3C zFR5wJpn*Y-OAsSd5?9?7i?2}O&#xOMJa_I~kIQIc7eeWPYa?)o;v$uz+GuKW=g#dv zZCK^p1dJdJ#hQ$c$LM)(yruOmGS6_|d@OjZP67pzrXIqN3F2QjNPQtMQ8&RRWz zpD;ZlMvopX0i|kMGAf%eBe;~IN|s8GR|f4jUoty(?0~N=z`Mhg$wEB;{PThC_FAV- z9d)n~WV&&KMW1&G7gip|MP6yDAV_M87cVYn z7g;acDY9sPbEp%aE z_8%pY!8)bJo`GBkH$XANT8&^HSQ)n(jz{0HtVsk*g6q`6MAnNQ%7=+(!aX?pY`aa> zXJ6=}FF($Pg$Rm5!ee?1p-XsUJTrm|9cJ7CTa z&8d=O#cHqBf0yOIGJI27Xe}H* zRN;PQ(RPiIDv;*UJrwzZCBjC9ot^gbi(c{m0DxaD`RE!FlzH*|sjnnO77 zlglB6(trC{M`9WUWyq%%7yrLt`T~Ucpa2|u1(yy6wG|R_;80X1G*d1m%SkORK3xS$ z8bXKR*!XE7Qj3c*$sF~6hqhis$Fh#JAX48n;6edwP8> + /// Return negative of Bitmap + /// + /// Bitmap to create a negative off + /// Negative bitmap + public static Bitmap CreateNegative(Bitmap sourceBitmap) { + using (BitmapBuffer bb = new BitmapBuffer(sourceBitmap, true)) { + bb.Lock(); + for (int y = 0; y < bb.Height; y++) { + for (int x = 0; x < bb.Width; x++) { + Color color = bb.GetColorAt(x, y); + Color invertedColor = Color.FromArgb(color.A, color.R ^ 255, color.G ^ 255, color.B ^ 255); + bb.SetColorAt(x, y, invertedColor); + } + } + bb.Unlock(); + return bb.Bitmap; + } + } + /// /// Create a new bitmap where the sourceBitmap has a Simple border around it ///