Fixed shadow with transparency issue.

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2095 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2012-09-25 12:19:22 +00:00
parent 9488200886
commit d8cb00e70c
4 changed files with 49 additions and 51 deletions

View file

@ -605,16 +605,14 @@ namespace Greenshot.Drawing {
try { try {
Rectangle imageRectangle = new Rectangle(Point.Empty, Image.Size); Rectangle imageRectangle = new Rectangle(Point.Empty, Image.Size);
Bitmap newImage = null; Bitmap newImage = null;
Point offset = Point.Empty; Point offset = new Point(-1,-1);
switch (effect) { switch (effect) {
case Effects.Shadow: case Effects.Shadow:
offset = new Point(6, 6); newImage = ImageHelper.CreateShadow((Bitmap)Image, 1f, 9, ref offset, PixelFormat.Format32bppArgb); //Image.PixelFormat);
newImage = ImageHelper.CreateShadow((Bitmap)Image, 1f, 7, offset, PixelFormat.Format24bppRgb); //Image.PixelFormat);
break; break;
case Effects.TornEdge: case Effects.TornEdge:
offset = new Point(5, 5);
using (Bitmap tmpImage = ImageHelper.CreateTornEdge((Bitmap)Image)) { using (Bitmap tmpImage = ImageHelper.CreateTornEdge((Bitmap)Image)) {
newImage = ImageHelper.CreateShadow(tmpImage, 1f, 6, offset, PixelFormat.Format24bppRgb); //Image.PixelFormat); newImage = ImageHelper.CreateShadow(tmpImage, 1f, 6, ref offset, PixelFormat.Format32bppArgb); //Image.PixelFormat);
} }
break; break;
case Effects.Border: case Effects.Border:
@ -971,7 +969,7 @@ namespace Greenshot.Drawing {
// Draw a checkboard when capturing with transparency // Draw a checkboard when capturing with transparency
protected override void OnPaintBackground(PaintEventArgs e) { protected override void OnPaintBackground(PaintEventArgs e) {
// check if we need to draw the checkerboard // check if we need to draw the checkerboard
if (Image.PixelFormat == PixelFormat.Format32bppArgb && transparencyBackgroundBrush != null) { if (Image.IsAlphaPixelFormat(Image.PixelFormat) && transparencyBackgroundBrush != null) {
Graphics targetGraphics = e.Graphics; Graphics targetGraphics = e.Graphics;
Rectangle clipRectangle = e.ClipRectangle; Rectangle clipRectangle = e.ClipRectangle;
targetGraphics.FillRectangle(transparencyBackgroundBrush, clipRectangle); targetGraphics.FillRectangle(transparencyBackgroundBrush, clipRectangle);

View file

@ -1109,8 +1109,8 @@ namespace Greenshot {
if (capture!= null && capture.Image != null) { if (capture!= null && capture.Image != null) {
bool addShadow = false; bool addShadow = false;
if (addShadow) { if (addShadow) {
Point offset = new Point(6, 6); Point offset = new Point(-1,-1);
using (Bitmap shadowImage = ImageHelper.CreateShadow(capture.Image, 1f, 7, offset, PixelFormat.Format32bppArgb)) { using (Bitmap shadowImage = ImageHelper.CreateShadow(capture.Image, 1f, 7, ref offset, PixelFormat.Format32bppArgb)) {
surface.AddBitmapContainer(shadowImage, 100, 100); surface.AddBitmapContainer(shadowImage, 100, 100);
} }
} else { } else {

View file

@ -422,6 +422,7 @@ namespace GreenshotPlugin.Core {
// aIndex is only set if the pixel format supports "A". // aIndex is only set if the pixel format supports "A".
aIndex = -1; aIndex = -1;
switch(bitmap.PixelFormat) { switch(bitmap.PixelFormat) {
case PixelFormat.Format32bppPArgb:
case PixelFormat.Format32bppArgb: case PixelFormat.Format32bppArgb:
bIndex = 0; bIndex = 0;
gIndex = 1; gIndex = 1;

View file

@ -428,7 +428,7 @@ namespace GreenshotPlugin.Core {
} }
byte[] nullColor = new byte[] { 255, 255, 255, 255 }; byte[] nullColor = new byte[] { 255, 255, 255, 255 };
if (sourceBitmap.PixelFormat == PixelFormat.Format32bppArgb) { if (sourceBitmap.PixelFormat == PixelFormat.Format32bppArgb || sourceBitmap.PixelFormat == PixelFormat.Format32bppPArgb) {
nullColor = new byte[] { 0, 0, 0, 0 }; nullColor = new byte[] { 0, 0, 0, 0 };
} }
byte[] settingColor = new byte[4]; byte[] settingColor = new byte[4];
@ -724,53 +724,51 @@ namespace GreenshotPlugin.Core {
/// <param name="targetPixelformat">What pixel format must the returning bitmap have</param> /// <param name="targetPixelformat">What pixel format must the returning bitmap have</param>
/// <param name="offset">How many pixels is the original image moved?</param> /// <param name="offset">How many pixels is the original image moved?</param>
/// <returns>Bitmap with the shadow, is bigger than the sourceBitmap!!</returns> /// <returns>Bitmap with the shadow, is bigger than the sourceBitmap!!</returns>
public static Bitmap CreateShadow(Image sourceBitmap, float darkness, int shadowSize, Point offset, PixelFormat targetPixelformat) { public static Bitmap CreateShadow(Image sourceBitmap, float darkness, int shadowSize, ref Point offset, PixelFormat targetPixelformat) {
// Create a new "clean" image // Create a new "clean" image
Bitmap newImage = CreateEmpty(sourceBitmap.Width + (shadowSize * 2), sourceBitmap.Height + (shadowSize * 2), targetPixelformat, Color.Empty, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution); Bitmap returnImage = null;
offset.X += shadowSize - 1;
offset.Y += shadowSize - 1;
using (Bitmap tmpImage = CreateEmpty(sourceBitmap.Width + (shadowSize * 2), sourceBitmap.Height + (shadowSize * 2), targetPixelformat, Color.Empty, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution)) {
using (Graphics graphics = Graphics.FromImage(tmpImage)) {
// Make sure we draw with the best quality!
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
using (Graphics graphics = Graphics.FromImage(newImage)) { // Draw "shadow" offsetted
// Make sure we draw with the best quality! ImageAttributes ia = new ImageAttributes();
graphics.SmoothingMode = SmoothingMode.HighQuality; ColorMatrix cm = new ColorMatrix();
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; cm.Matrix00 = 0;
graphics.CompositingQuality = CompositingQuality.HighQuality; cm.Matrix11 = 0;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; cm.Matrix22 = 0;
cm.Matrix33 = darkness;
// Draw "shadow" offsetted ia.SetColorMatrix(cm);
ImageAttributes ia = new ImageAttributes(); Rectangle shadowRectangle = new Rectangle(new Point(shadowSize - 1, shadowSize - 1), sourceBitmap.Size);
ColorMatrix cm = new ColorMatrix(); graphics.DrawImage(sourceBitmap, shadowRectangle, 0, 0, sourceBitmap.Width, sourceBitmap.Height, GraphicsUnit.Pixel, ia);
cm.Matrix00 = 0;
cm.Matrix11 = 0;
cm.Matrix22 = 0;
cm.Matrix33 = darkness;
ia.SetColorMatrix(cm);
Rectangle shadowRectangle = new Rectangle(new Point(shadowSize, shadowSize), sourceBitmap.Size);
graphics.DrawImage(sourceBitmap, shadowRectangle, 0, 0, sourceBitmap.Width, sourceBitmap.Height, GraphicsUnit.Pixel, ia);
// Only do the blur on the edges
//Rectangle blurRectangle = new Rectangle(shadowSize + 30, shadowSize + 30, sourceBitmap.Width - 60, sourceBitmap.Height - 60);
//Rectangle applyRect = ImageHelper.CreateIntersectRectangle(newImage.Size, blurRectangle, true);
//LOG.DebugFormat("blurRect = {0} - applyRect = {1}", blurRectangle, applyRect);
//using (Bitmap blurImage = ImageHelper.CreateBlur(newImage, applyRect, true, shadowSize, 1d, true, blurRectangle)) {
// if (blurImage != null) {
// graphics.DrawImageUnscaled(blurImage, applyRect.Location);
// }
//}
// blur "shadow", apply to whole new image
Rectangle newImageRectangle = new Rectangle(0, 0, newImage.Width, newImage.Height);
//using (Bitmap blurImage = FastBlur(newImage, shadowSize-1)) {
using (Bitmap blurImage = CreateBlur(newImage, newImageRectangle, true, shadowSize, 1d, false, newImageRectangle)) {
graphics.DrawImageUnscaled(blurImage, newImageRectangle.Location);
} }
// blur "shadow", apply to whole new image
// draw original with a TextureBrush so we have nice antialiasing! Rectangle newImageRectangle = new Rectangle(0, 0, tmpImage.Width, tmpImage.Height);
using (Brush textureBrush = new TextureBrush(sourceBitmap, WrapMode.Clamp)) { //using (Bitmap blurImage = FastBlur(newImage, shadowSize-1)) {
// We need to do a translate-tranform otherwise the image is wrapped returnImage = CreateBlur(tmpImage, newImageRectangle, true, shadowSize, 1d, false, newImageRectangle);
graphics.TranslateTransform(offset.X, offset.Y); }
graphics.FillRectangle(textureBrush, 0, 0, sourceBitmap.Width, sourceBitmap.Height); if (returnImage != null) {
using (Graphics graphics = Graphics.FromImage(returnImage)) {
// Make sure we draw with the best quality!
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
// draw original with a TextureBrush so we have nice antialiasing!
using (Brush textureBrush = new TextureBrush(sourceBitmap, WrapMode.Clamp)) {
// We need to do a translate-tranform otherwise the image is wrapped
graphics.TranslateTransform(offset.X, offset.Y);
graphics.FillRectangle(textureBrush, 0, 0, sourceBitmap.Width, sourceBitmap.Height);
}
} }
} }
return newImage; return returnImage;
} }
/// <summary> /// <summary>
@ -883,6 +881,7 @@ namespace GreenshotPlugin.Core {
/// <returns>bool if we support it</returns> /// <returns>bool if we support it</returns>
public static bool SupportsPixelFormat(PixelFormat pixelformat) { public static bool SupportsPixelFormat(PixelFormat pixelformat) {
return (pixelformat.Equals(PixelFormat.Format32bppArgb) || return (pixelformat.Equals(PixelFormat.Format32bppArgb) ||
pixelformat.Equals(PixelFormat.Format32bppPArgb) ||
pixelformat.Equals(PixelFormat.Format32bppRgb) || pixelformat.Equals(PixelFormat.Format32bppRgb) ||
pixelformat.Equals(PixelFormat.Format24bppRgb)); pixelformat.Equals(PixelFormat.Format24bppRgb));
} }