mirror of
https://github.com/greenshot/greenshot
synced 2025-08-13 18:27:03 -07:00
Changed code for the Clipboard and changed a lot of "Bitmap" code to work on images, this might give problems but if these are solved we have better support for MetaFiles. This made it possible to remove the MetafileContainer, which is replaced by the ImageContainer (which is the BitmapContainer modified). The clipboard code now knows more ways to load images from the clipboard!
git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2398 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
parent
b7a604d93b
commit
f627ce5dd0
15 changed files with 301 additions and 382 deletions
|
@ -125,8 +125,8 @@ namespace Greenshot.Destinations {
|
|||
}
|
||||
} else {
|
||||
try {
|
||||
using (Bitmap image = (Bitmap)surface.GetImageForExport()) {
|
||||
editor.Surface.AddBitmapContainer(image, 10, 10);
|
||||
using (Image image = surface.GetImageForExport()) {
|
||||
editor.Surface.AddImageContainer(image, 10, 10);
|
||||
}
|
||||
exportInformation.ExportMade = true;
|
||||
} catch (Exception e) {
|
||||
|
|
|
@ -36,17 +36,17 @@ namespace Greenshot.Drawing {
|
|||
/// Description of BitmapContainer.
|
||||
/// </summary>
|
||||
[Serializable()]
|
||||
public class BitmapContainer : DrawableContainer, IBitmapContainer {
|
||||
private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(BitmapContainer));
|
||||
public class ImageContainer : DrawableContainer, IImageContainer {
|
||||
private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(ImageContainer));
|
||||
|
||||
private Bitmap bitmap;
|
||||
private Image image;
|
||||
|
||||
/// <summary>
|
||||
/// This is the shadow version of the bitmap, rendered once to save performance
|
||||
/// Do not serialize, as the shadow is recreated from the original bitmap if it's not available
|
||||
/// </summary>
|
||||
[NonSerialized]
|
||||
private Bitmap shadowBitmap = null;
|
||||
private Image shadowBitmap = null;
|
||||
|
||||
/// <summary>
|
||||
/// This is the offset for the shadow version of the bitmap
|
||||
|
@ -55,11 +55,11 @@ namespace Greenshot.Drawing {
|
|||
[NonSerialized]
|
||||
private Point shadowOffset = new Point(-1, -1);
|
||||
|
||||
public BitmapContainer(Surface parent, string filename) : this(parent) {
|
||||
public ImageContainer(Surface parent, string filename) : this(parent) {
|
||||
Load(filename);
|
||||
}
|
||||
|
||||
public BitmapContainer(Surface parent) : base(parent) {
|
||||
public ImageContainer(Surface parent) : base(parent) {
|
||||
AddField(GetType(), FieldType.SHADOW, false);
|
||||
FieldChanged += BitmapContainer_OnFieldChanged;
|
||||
}
|
||||
|
@ -81,8 +81,8 @@ namespace Greenshot.Drawing {
|
|||
this.Left = this.Left - this.shadowOffset.X;
|
||||
this.Top = this.Top - this.shadowOffset.Y;
|
||||
} else {
|
||||
this.Width = bitmap.Width;
|
||||
this.Height = bitmap.Height;
|
||||
this.Width = image.Width;
|
||||
this.Height = image.Height;
|
||||
if (shadowBitmap != null) {
|
||||
this.Left = this.Left + this.shadowOffset.X;
|
||||
this.Top = this.Top + this.shadowOffset.Y;
|
||||
|
@ -90,16 +90,16 @@ namespace Greenshot.Drawing {
|
|||
}
|
||||
}
|
||||
|
||||
public Bitmap Bitmap {
|
||||
public Image Image {
|
||||
set {
|
||||
// Remove all current bitmaps
|
||||
Dispose(true);
|
||||
bitmap = ImageHelper.Clone(value);
|
||||
image = ImageHelper.Clone(value);
|
||||
bool shadow = GetFieldValueAsBool(FieldType.SHADOW);
|
||||
CheckShadow(shadow);
|
||||
if (!shadow) {
|
||||
Width = bitmap.Width;
|
||||
Height = bitmap.Height;
|
||||
Width = image.Width;
|
||||
Height = image.Height;
|
||||
} else {
|
||||
Width = shadowBitmap.Width;
|
||||
Height = shadowBitmap.Height;
|
||||
|
@ -107,13 +107,13 @@ namespace Greenshot.Drawing {
|
|||
this.Top = this.Top - this.shadowOffset.Y;
|
||||
}
|
||||
}
|
||||
get { return bitmap; }
|
||||
get { return image; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Destructor
|
||||
/// </summary>
|
||||
~BitmapContainer() {
|
||||
~ImageContainer() {
|
||||
Dispose(false);
|
||||
}
|
||||
|
||||
|
@ -135,14 +135,14 @@ namespace Greenshot.Drawing {
|
|||
/// <param name="disposing"></param>
|
||||
protected virtual void Dispose(bool disposing) {
|
||||
if (disposing) {
|
||||
if (bitmap != null) {
|
||||
bitmap.Dispose();
|
||||
if (image != null) {
|
||||
image.Dispose();
|
||||
}
|
||||
if (shadowBitmap != null) {
|
||||
shadowBitmap.Dispose();
|
||||
}
|
||||
}
|
||||
bitmap = null;
|
||||
image = null;
|
||||
shadowBitmap = null;
|
||||
}
|
||||
|
||||
|
@ -154,8 +154,8 @@ namespace Greenshot.Drawing {
|
|||
if (File.Exists(filename)) {
|
||||
// Always make sure ImageHelper.LoadBitmap results are disposed some time,
|
||||
// as we close the bitmap internally, we need to do it afterwards
|
||||
using (Bitmap tmpImage = ImageHelper.LoadBitmap(filename)) {
|
||||
Bitmap = tmpImage;
|
||||
using (Image tmpImage = ImageHelper.LoadImage(filename)) {
|
||||
Image = tmpImage;
|
||||
}
|
||||
LOG.Debug("Loaded file: " + filename + " with resolution: " + Height + "," + Width);
|
||||
}
|
||||
|
@ -166,11 +166,11 @@ namespace Greenshot.Drawing {
|
|||
/// </summary>
|
||||
/// <param name="rotateFlipType"></param>
|
||||
public override void Rotate(RotateFlipType rotateFlipType) {
|
||||
Bitmap newBitmap = ImageHelper.RotateFlip((Bitmap)bitmap, rotateFlipType);
|
||||
if (newBitmap != null) {
|
||||
Image newImage = ImageHelper.RotateFlip((Bitmap)image, rotateFlipType);
|
||||
if (newImage != null) {
|
||||
// Remove all current bitmaps, also the shadow (will be recreated)
|
||||
Dispose(true);
|
||||
bitmap = newBitmap;
|
||||
image = newImage;
|
||||
}
|
||||
base.Rotate(rotateFlipType);
|
||||
}
|
||||
|
@ -181,7 +181,7 @@ namespace Greenshot.Drawing {
|
|||
/// <param name="shadow"></param>
|
||||
private void CheckShadow(bool shadow) {
|
||||
if (shadow && shadowBitmap == null) {
|
||||
shadowBitmap = ImageHelper.ApplyEffect(bitmap, new DropShadowEffect(), out shadowOffset);
|
||||
shadowBitmap = ImageHelper.ApplyEffect(image, new DropShadowEffect(), out shadowOffset);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -191,7 +191,7 @@ namespace Greenshot.Drawing {
|
|||
/// <param name="graphics"></param>
|
||||
/// <param name="rm"></param>
|
||||
public override void Draw(Graphics graphics, RenderMode rm) {
|
||||
if (bitmap != null) {
|
||||
if (image != null) {
|
||||
bool shadow = GetFieldValueAsBool(FieldType.SHADOW);
|
||||
graphics.SmoothingMode = SmoothingMode.HighQuality;
|
||||
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
||||
|
@ -202,7 +202,7 @@ namespace Greenshot.Drawing {
|
|||
CheckShadow(shadow);
|
||||
graphics.DrawImage(shadowBitmap, Bounds);
|
||||
} else {
|
||||
graphics.DrawImage(bitmap, Bounds);
|
||||
graphics.DrawImage(image, Bounds);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -215,7 +215,7 @@ namespace Greenshot.Drawing {
|
|||
|
||||
public override Size DefaultSize {
|
||||
get {
|
||||
return bitmap.Size;
|
||||
return image.Size;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,123 +0,0 @@
|
|||
/*
|
||||
* Greenshot - a free and open source screenshot tool
|
||||
* Copyright (C) 2007-2012 Thomas Braun, Jens Klingen, Robin Krom
|
||||
*
|
||||
* For more information see: http://getgreenshot.org/
|
||||
* The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 1 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
|
||||
using Greenshot.Plugin.Drawing;
|
||||
using System.Drawing.Drawing2D;
|
||||
|
||||
namespace Greenshot.Drawing {
|
||||
/// <summary>
|
||||
/// Description of MetafileContainer.
|
||||
/// </summary>
|
||||
[Serializable()]
|
||||
public class MetafileContainer : DrawableContainer, IMetafileContainer {
|
||||
private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(MetafileContainer));
|
||||
|
||||
protected Metafile metafile;
|
||||
|
||||
public MetafileContainer(Surface parent) : base(parent) {
|
||||
}
|
||||
|
||||
public MetafileContainer(Surface parent, string filename) : base(parent) {
|
||||
Load(filename);
|
||||
}
|
||||
|
||||
public Metafile Metafile {
|
||||
set {
|
||||
if (metafile != null) {
|
||||
metafile.Dispose();
|
||||
}
|
||||
metafile = (Metafile)value.Clone();
|
||||
Height = Math.Abs(value.Height);
|
||||
if (Height == 0 ) {
|
||||
Height = 100;
|
||||
}
|
||||
Width = Math.Abs(value.Width);
|
||||
if (Width == 0 ) {
|
||||
Width = 100;
|
||||
}
|
||||
while (Height > parent.Height) {
|
||||
Height = Height / 4;
|
||||
Width = Width / 4;
|
||||
}
|
||||
while (Width > parent.Width) {
|
||||
Height = Height / 4;
|
||||
Width = Width / 4;
|
||||
}
|
||||
}
|
||||
get { return metafile; }
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor
|
||||
*/
|
||||
~MetafileContainer() {
|
||||
Dispose(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* The public accessible Dispose
|
||||
* Will call the GarbageCollector to SuppressFinalize, preventing being cleaned twice
|
||||
*/
|
||||
public new void Dispose() {
|
||||
Dispose(true);
|
||||
base.Dispose();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
// The bulk of the clean-up code is implemented in Dispose(bool)
|
||||
|
||||
/**
|
||||
* This Dispose is called from the Dispose and the Destructor.
|
||||
* When disposing==true all non-managed resources should be freed too!
|
||||
*/
|
||||
protected virtual void Dispose(bool disposing) {
|
||||
if (disposing) {
|
||||
if (metafile != null) {
|
||||
metafile.Dispose();
|
||||
}
|
||||
}
|
||||
metafile = null;
|
||||
}
|
||||
|
||||
public void Load(string filename) {
|
||||
if (File.Exists(filename)) {
|
||||
using (Stream imageFileStream = File.OpenRead(filename)) {
|
||||
Metafile = (Metafile)Image.FromStream(imageFileStream, true, true);
|
||||
LOG.Debug("Loaded file: " + filename + " with resolution: " + Height + "," + Width);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void Draw(Graphics graphics, RenderMode rm) {
|
||||
if (metafile != null) {
|
||||
graphics.SmoothingMode = SmoothingMode.HighQuality;
|
||||
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
||||
graphics.CompositingQuality = CompositingQuality.HighQuality;
|
||||
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
|
||||
graphics.DrawImage(metafile, Bounds);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -620,7 +620,7 @@ namespace Greenshot.Drawing {
|
|||
undrawnElement = cropContainer;
|
||||
break;
|
||||
case DrawingModes.Bitmap:
|
||||
undrawnElement = new BitmapContainer(this);
|
||||
undrawnElement = new ImageContainer(this);
|
||||
break;
|
||||
case DrawingModes.Path:
|
||||
undrawnElement = new FreehandContainer(this);
|
||||
|
@ -635,17 +635,17 @@ namespace Greenshot.Drawing {
|
|||
}
|
||||
|
||||
#region Plugin interface implementations
|
||||
public IBitmapContainer AddBitmapContainer(Bitmap bitmap, int x, int y) {
|
||||
BitmapContainer bitmapContainer = new BitmapContainer(this);
|
||||
bitmapContainer.Bitmap = bitmap;
|
||||
public IImageContainer AddImageContainer(Image image, int x, int y) {
|
||||
ImageContainer bitmapContainer = new ImageContainer(this);
|
||||
bitmapContainer.Image = image;
|
||||
bitmapContainer.Left = x;
|
||||
bitmapContainer.Top = y;
|
||||
AddElement(bitmapContainer);
|
||||
return bitmapContainer;
|
||||
}
|
||||
|
||||
public IBitmapContainer AddBitmapContainer(string filename, int x, int y) {
|
||||
BitmapContainer bitmapContainer = new BitmapContainer(this);
|
||||
public IImageContainer AddImageContainer(string filename, int x, int y) {
|
||||
ImageContainer bitmapContainer = new ImageContainer(this);
|
||||
bitmapContainer.Load(filename);
|
||||
bitmapContainer.Left = x;
|
||||
bitmapContainer.Top = y;
|
||||
|
@ -684,22 +684,6 @@ namespace Greenshot.Drawing {
|
|||
AddElement(cursorContainer);
|
||||
return cursorContainer;
|
||||
}
|
||||
public IMetafileContainer AddMetafileContainer(string filename, int x, int y) {
|
||||
MetafileContainer metafileContainer = new MetafileContainer(this);
|
||||
metafileContainer.Load(filename);
|
||||
metafileContainer.Left = x;
|
||||
metafileContainer.Top = y;
|
||||
AddElement(metafileContainer);
|
||||
return metafileContainer;
|
||||
}
|
||||
public IMetafileContainer AddMetafileContainer(Metafile metafile, int x, int y) {
|
||||
MetafileContainer metafileContainer = new MetafileContainer(this);
|
||||
metafileContainer.Metafile = metafile;
|
||||
metafileContainer.Left = x;
|
||||
metafileContainer.Top = y;
|
||||
AddElement(metafileContainer);
|
||||
return metafileContainer;
|
||||
}
|
||||
|
||||
public ITextContainer AddTextContainer(string text, HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment, FontFamily family, float size, bool italic, bool bold, bool shadow, int borderSize, Color color, Color fillColor) {
|
||||
TextContainer textContainer = new TextContainer(this);
|
||||
|
@ -735,11 +719,10 @@ namespace Greenshot.Drawing {
|
|||
if (draggingInProgress || (e.AllowedEffect & DragDropEffects.Copy) != DragDropEffects.Copy) {
|
||||
e.Effect=DragDropEffects.None;
|
||||
} else {
|
||||
List<string> filenames = ClipboardHelper.GetImageFilenames(e.Data);
|
||||
if ((filenames != null && filenames.Count > 0) || ClipboardHelper.ContainsImage(e.Data) || ClipboardHelper.ContainsFormat(e.Data, "DragImageBits")) {
|
||||
e.Effect=DragDropEffects.Copy;
|
||||
if (ClipboardHelper.ContainsImage(e.Data) || ClipboardHelper.ContainsFormat(e.Data, "DragImageBits")) {
|
||||
e.Effect = DragDropEffects.Copy;
|
||||
} else {
|
||||
e.Effect=DragDropEffects.None;
|
||||
e.Effect = DragDropEffects.None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -756,31 +739,19 @@ namespace Greenshot.Drawing {
|
|||
string possibleUrl = ClipboardHelper.GetText(e.Data);
|
||||
// Test if it's an url and try to download the image so we have it in the original form
|
||||
if (possibleUrl != null && possibleUrl.StartsWith("http")) {
|
||||
using (Bitmap image = NetworkHelper.DownloadImage(possibleUrl)) {
|
||||
using (Image image = NetworkHelper.DownloadImage(possibleUrl)) {
|
||||
if (image != null) {
|
||||
AddBitmapContainer(image, mouse.X, mouse.Y);
|
||||
AddImageContainer(image, mouse.X, mouse.Y);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ((filenames != null && filenames.Count > 0)) {
|
||||
foreach (string filename in filenames) {
|
||||
if (filename != null && filename.Trim().Length > 0) {
|
||||
LOG.Debug("Drop - filename: " + filename);
|
||||
if (filename.ToLower().EndsWith("wmf")) {
|
||||
AddMetafileContainer(filename, mouse.X, mouse.Y);
|
||||
} else {
|
||||
AddBitmapContainer(filename, mouse.X, mouse.Y);
|
||||
}
|
||||
foreach (Image image in ClipboardHelper.GetImages(e.Data)) {
|
||||
AddImageContainer(image, mouse.X, mouse.Y);
|
||||
mouse.Offset(10, 10);
|
||||
}
|
||||
}
|
||||
} else if (e.Data.GetDataPresent(DataFormats.Bitmap)) {
|
||||
AddBitmapContainer((Bitmap)e.Data.GetData(DataFormats.Bitmap, true), mouse.X, mouse.Y);
|
||||
} else if (e.Data.GetDataPresent(DataFormats.EnhancedMetafile)) {
|
||||
AddMetafileContainer((Metafile)e.Data.GetData(DataFormats.EnhancedMetafile, true), mouse.X, mouse.Y);
|
||||
image.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -847,7 +818,7 @@ namespace Greenshot.Drawing {
|
|||
try {
|
||||
Rectangle imageRectangle = new Rectangle(Point.Empty, Image.Size);
|
||||
Point offset;
|
||||
Bitmap newImage = ImageHelper.ApplyEffect((Bitmap)Image, effect, out offset);
|
||||
Image newImage = ImageHelper.ApplyEffect(Image, effect, out offset);
|
||||
if (newImage != null) {
|
||||
// Make sure the elements move according to the offset the effect made the bitmap move
|
||||
elements.MoveBy(offset.X, offset.Y);
|
||||
|
@ -1149,7 +1120,7 @@ namespace Greenshot.Drawing {
|
|||
/// <returns></returns>
|
||||
private Image GetImage(RenderMode renderMode) {
|
||||
// Generate a copy of the original image with a dpi equal to the default...
|
||||
Bitmap clone = ImageHelper.Clone(image);
|
||||
Bitmap clone = ImageHelper.Clone(image, PixelFormat.DontCare);
|
||||
// otherwise we would have a problem drawing the image to the surface... :(
|
||||
using (Graphics graphics = Graphics.FromImage(clone)) {
|
||||
// Do not set the following, the containers need to decide themselves
|
||||
|
@ -1391,11 +1362,16 @@ namespace Greenshot.Drawing {
|
|||
SelectElements(dcs);
|
||||
}
|
||||
} else if (ClipboardHelper.ContainsImage(clipboard)) {
|
||||
using (Image clipboardImage = ClipboardHelper.GetImage(clipboard)) {
|
||||
int x = 10;
|
||||
int y = 10;
|
||||
foreach (Image clipboardImage in ClipboardHelper.GetImages(clipboard)) {
|
||||
if (clipboardImage != null) {
|
||||
DeselectAllElements();
|
||||
IBitmapContainer bitmapContainer = AddBitmapContainer(clipboardImage as Bitmap, 0, 0);
|
||||
SelectElement(bitmapContainer);
|
||||
IImageContainer container = AddImageContainer(clipboardImage as Bitmap, x, y);
|
||||
SelectElement(container);
|
||||
clipboardImage.Dispose();
|
||||
x += 10;
|
||||
y += 10;
|
||||
}
|
||||
}
|
||||
} else if (ClipboardHelper.ContainsText(clipboard)) {
|
||||
|
|
|
@ -1180,7 +1180,7 @@ namespace Greenshot {
|
|||
this.Activate();
|
||||
WindowDetails.ToForeground(this.Handle);
|
||||
if (capture!= null && capture.Image != null) {
|
||||
surface.AddBitmapContainer((Bitmap)capture.Image, 100, 100);
|
||||
surface.AddImageContainer((Bitmap)capture.Image, 100, 100);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -328,7 +328,7 @@ namespace Greenshot.Helpers {
|
|||
}
|
||||
break;
|
||||
case CaptureMode.File:
|
||||
Bitmap fileBitmap = null;
|
||||
Image fileImage = null;
|
||||
string filename = capture.CaptureDetails.Filename;
|
||||
|
||||
if (!string.IsNullOrEmpty(filename)) {
|
||||
|
@ -345,20 +345,20 @@ namespace Greenshot.Helpers {
|
|||
MessageBox.Show(Language.GetFormattedString(LangKey.error_openfile, filename));
|
||||
}
|
||||
try {
|
||||
fileBitmap = ImageHelper.LoadBitmap(filename);
|
||||
fileImage = ImageHelper.LoadImage(filename);
|
||||
} catch (Exception e) {
|
||||
LOG.Error(e.Message, e);
|
||||
MessageBox.Show(Language.GetFormattedString(LangKey.error_openfile, filename));
|
||||
}
|
||||
}
|
||||
if (fileBitmap != null) {
|
||||
if (fileImage != null) {
|
||||
capture.CaptureDetails.Title = Path.GetFileNameWithoutExtension(filename);
|
||||
capture.CaptureDetails.AddMetaData("file", filename);
|
||||
capture.CaptureDetails.AddMetaData("source", "file");
|
||||
if (capture != null) {
|
||||
capture.Image = fileBitmap;
|
||||
capture.Image = fileImage;
|
||||
} else {
|
||||
capture = new Capture(fileBitmap);
|
||||
capture = new Capture(fileImage);
|
||||
}
|
||||
// Force Editor, keep picker, this is currently the only usefull destination
|
||||
if (capture.CaptureDetails.HasDestination(PickerDestination.DESIGNATION)) {
|
||||
|
|
|
@ -108,7 +108,7 @@ namespace GreenshotPlugin.Core {
|
|||
/// Create a BitmapBuffer from a Bitmap
|
||||
/// </summary>
|
||||
/// <param name="sourceBmp">Bitmap</param>
|
||||
public BitmapBuffer(Bitmap bmp) : this(bmp, Rectangle.Empty) {
|
||||
public BitmapBuffer(Image sourceBmp) : this(sourceBmp, Rectangle.Empty) {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -116,7 +116,7 @@ namespace GreenshotPlugin.Core {
|
|||
/// </summary>
|
||||
/// <param name="sourceBmp">Bitmap</param>
|
||||
/// <param name="clone">bool specifying if the bitmap needs to be cloned</param>
|
||||
public BitmapBuffer(Bitmap sourceBmp, bool clone) : this(sourceBmp, Rectangle.Empty, clone) {
|
||||
public BitmapBuffer(Image sourceBmp, bool clone) : this(sourceBmp, Rectangle.Empty, clone) {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -124,21 +124,21 @@ namespace GreenshotPlugin.Core {
|
|||
/// </summary>
|
||||
/// <param name="sourceBmp">Bitmap</param>
|
||||
/// <param name="applyRect">Rectangle</param>
|
||||
public BitmapBuffer(Bitmap sourceBmp, Rectangle applyRect) : this(sourceBmp, applyRect, true) {
|
||||
public BitmapBuffer(Image sourceBmp, Rectangle applyRect) : this(sourceBmp, applyRect, true) {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a BitmapBuffer from a Bitmap, a Rectangle specifying what part from the Bitmap to take and a flag if we need a clone
|
||||
/// </summary>
|
||||
/// <param name="sourceBmp">Bitmap</param>
|
||||
/// <param name="sourceImage">Image, will be cloned if it's not a bitmap!!!</param>
|
||||
/// <param name="applyRect">Rectangle</param>
|
||||
/// <param name="clone">bool specifying if the bitmap needs to be cloned</param>
|
||||
public BitmapBuffer(Bitmap sourceBmp, Rectangle applyRect, bool clone) {
|
||||
public BitmapBuffer(Image sourceImage, Rectangle applyRect, bool clone) {
|
||||
this.clone = clone;
|
||||
Rectangle sourceRect = new Rectangle(applyRect.X, applyRect.Y, applyRect.Width, applyRect.Height);
|
||||
Rectangle bitmapRect = new Rectangle(0,0, sourceBmp.Width, sourceBmp.Height);
|
||||
Rectangle bitmapRect = new Rectangle(0,0, sourceImage.Width, sourceImage.Height);
|
||||
|
||||
if(sourceRect.IsEmpty) {
|
||||
if (sourceRect.IsEmpty) {
|
||||
sourceRect = bitmapRect;
|
||||
} else {
|
||||
sourceRect.Intersect(bitmapRect);
|
||||
|
@ -148,15 +148,20 @@ namespace GreenshotPlugin.Core {
|
|||
return;
|
||||
}
|
||||
|
||||
if (!(sourceImage is Bitmap) && !clone) {
|
||||
LOG.Warn("Chancing clone to true, as the image is not a bitmap");
|
||||
clone = true;
|
||||
}
|
||||
|
||||
if (clone) {
|
||||
this.bitmap = ImageHelper.CloneArea(sourceBmp, sourceRect, PixelFormat.DontCare);
|
||||
this.bitmap = ImageHelper.CloneArea(sourceImage, sourceRect, PixelFormat.DontCare);
|
||||
// Set "this" rect to location 0,0
|
||||
// as the Cloned Bitmap is only the part we want to work with
|
||||
this.rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
|
||||
} else if (!ImageHelper.SupportsPixelFormat(sourceBmp) && PixelFormat.Format8bppIndexed != sourceBmp.PixelFormat) {
|
||||
throw new ArgumentException("Unsupported pixel format: " + sourceBmp.PixelFormat + " set clone to true!");
|
||||
} else if (!ImageHelper.SupportsPixelFormat(sourceImage) && PixelFormat.Format8bppIndexed != sourceImage.PixelFormat) {
|
||||
throw new ArgumentException("Unsupported pixel format: " + sourceImage.PixelFormat + " set clone to true!");
|
||||
} else {
|
||||
this.bitmap = sourceBmp;
|
||||
this.bitmap = sourceImage as Bitmap;
|
||||
this.rect = sourceRect;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -39,6 +39,9 @@ namespace GreenshotPlugin.Core {
|
|||
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(ClipboardHelper));
|
||||
private static readonly Object clipboardLockObject = new Object();
|
||||
private static readonly CoreConfiguration config = IniConfig.GetIniSection<CoreConfiguration>();
|
||||
private static readonly string FORMAT_FILECONTENTS = "FileContents";
|
||||
private static readonly string FORMAT_JPG = "JPG";
|
||||
private static readonly string FORMAT_PNG = "PNG";
|
||||
private static IntPtr nextClipboardViewer = IntPtr.Zero;
|
||||
// Template for the HTML Text on the clipboard
|
||||
// see: http://msdn.microsoft.com/en-us/library/ms649015%28v=vs.85%29.aspx
|
||||
|
@ -110,16 +113,6 @@ EndSelection:<<<<<<<4
|
|||
return owner;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wrapper for the SetDataObject with a bool "copy"
|
||||
/// Default is true, the information on the clipboard will stay even if our application quits
|
||||
/// For our own Types the other SetDataObject should be called with false.
|
||||
/// </summary>
|
||||
/// <param name="ido"></param>
|
||||
private static void SetDataObject(IDataObject ido) {
|
||||
SetDataObject(ido, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The SetDataObject will lock/try/catch clipboard operations making it save and not show exceptions.
|
||||
/// The bool "copy" is used to decided if the information stays on the clipboard after exit.
|
||||
|
@ -226,14 +219,26 @@ EndSelection:<<<<<<<4
|
|||
|| dataObject.GetDataPresent(DataFormats.Dib)
|
||||
|| dataObject.GetDataPresent(DataFormats.Tiff)
|
||||
|| dataObject.GetDataPresent(DataFormats.EnhancedMetafile)
|
||||
|| dataObject.GetDataPresent("PNG")
|
||||
|| dataObject.GetDataPresent("JPG")) {
|
||||
|| dataObject.GetDataPresent(FORMAT_PNG)
|
||||
|| dataObject.GetDataPresent(FORMAT_JPG)) {
|
||||
return true;
|
||||
}
|
||||
List<string> imageFiles = GetImageFilenames(dataObject);
|
||||
if (imageFiles != null && imageFiles.Count > 0) {
|
||||
return true;
|
||||
}
|
||||
if (dataObject.GetDataPresent(FORMAT_FILECONTENTS)) {
|
||||
try {
|
||||
MemoryStream imageStream = dataObject.GetData(FORMAT_FILECONTENTS) as MemoryStream;
|
||||
if (isValidStream(imageStream)) {
|
||||
using (Image tmpImage = Image.FromStream(imageStream)) {
|
||||
// If we get here, there is an image
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} catch (Exception) {
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
@ -253,25 +258,64 @@ EndSelection:<<<<<<<4
|
|||
/// <returns>Image if there is an image on the clipboard</returns>
|
||||
public static Image GetImage() {
|
||||
IDataObject clipboardData = GetDataObject();
|
||||
return GetImage(clipboardData);
|
||||
// Return the first image
|
||||
foreach (Image clipboardImage in GetImages(clipboardData)) {
|
||||
return clipboardImage;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get an Image from the IDataObject
|
||||
/// Get all images (multiple if filenames are available) from the dataObject
|
||||
/// Returned images must be disposed by the calling code!
|
||||
/// </summary>
|
||||
/// <param name="dataObject"></param>
|
||||
/// <returns>IEnumerable<Image></returns>
|
||||
public static IEnumerable<Image> GetImages(IDataObject dataObject) {
|
||||
Image tmpImage = null;
|
||||
Image returnImage = null;
|
||||
|
||||
// Get single image, this takes the "best" match
|
||||
tmpImage = GetImage(dataObject);
|
||||
if (tmpImage != null) {
|
||||
returnImage = ImageHelper.Clone(tmpImage);
|
||||
// Clean up.
|
||||
tmpImage.Dispose();
|
||||
yield return returnImage;
|
||||
} else {
|
||||
// check if files are supplied
|
||||
List<string> imageFiles = GetImageFilenames(dataObject);
|
||||
if (imageFiles != null) {
|
||||
foreach (string imageFile in imageFiles) {
|
||||
try {
|
||||
returnImage = ImageHelper.LoadImage(imageFile);
|
||||
} catch (Exception streamImageEx) {
|
||||
LOG.Error("Problem retrieving Image from clipboard.", streamImageEx);
|
||||
}
|
||||
if (returnImage != null) {
|
||||
yield return returnImage;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get an Image from the IDataObject, don't check for FileDrop
|
||||
/// </summary>
|
||||
/// <param name="dataObject"></param>
|
||||
/// <returns>Image or null</returns>
|
||||
public static Image GetImage(IDataObject dataObject) {
|
||||
private static Image GetImage(IDataObject dataObject) {
|
||||
if (dataObject != null) {
|
||||
IList<string> formats = GetFormats(dataObject);
|
||||
try {
|
||||
MemoryStream imageStream = null;
|
||||
|
||||
if (!isValidStream(imageStream) && formats.Contains("PNG")) {
|
||||
imageStream = GetFromDataObject(dataObject, "PNG") as MemoryStream;
|
||||
if (!isValidStream(imageStream) && formats.Contains(FORMAT_PNG)) {
|
||||
imageStream = GetFromDataObject(dataObject, FORMAT_PNG) as MemoryStream;
|
||||
}
|
||||
if (!isValidStream(imageStream) && formats.Contains("JPG")) {
|
||||
imageStream = GetFromDataObject(dataObject, "JPG") as MemoryStream;
|
||||
if (!isValidStream(imageStream) && formats.Contains(FORMAT_JPG)) {
|
||||
imageStream = GetFromDataObject(dataObject, FORMAT_JPG) as MemoryStream;
|
||||
}
|
||||
if (!isValidStream(imageStream) && formats.Contains(DataFormats.Tiff)) {
|
||||
imageStream = GetFromDataObject(dataObject, DataFormats.Tiff) as MemoryStream;
|
||||
|
@ -323,14 +367,16 @@ EndSelection:<<<<<<<4
|
|||
}
|
||||
}
|
||||
}
|
||||
List<string> imageFiles = GetImageFilenames(dataObject);
|
||||
if (imageFiles != null) {
|
||||
foreach (string imageFile in imageFiles) {
|
||||
if (File.Exists(imageFile)) {
|
||||
using (Stream imageFileStream = File.OpenRead(imageFile)) {
|
||||
return Image.FromStream(imageFileStream, true, true);
|
||||
}
|
||||
|
||||
// Support for FileContents
|
||||
imageStream = dataObject.GetData(FORMAT_FILECONTENTS) as MemoryStream;
|
||||
if (isValidStream(imageStream)) {
|
||||
try {
|
||||
using (Image tmpImage = Image.FromStream(imageStream)) {
|
||||
return ImageHelper.Clone(tmpImage);
|
||||
}
|
||||
} catch (Exception streamImageEx) {
|
||||
LOG.Error("Problem retrieving Image from clipboard.", streamImageEx);
|
||||
}
|
||||
}
|
||||
} catch (Exception dibEx) {
|
||||
|
@ -370,7 +416,7 @@ EndSelection:<<<<<<<4
|
|||
public static void SetClipboardData(string text) {
|
||||
IDataObject ido = new DataObject();
|
||||
ido.SetData(DataFormats.Text, true, text);
|
||||
SetDataObject(ido);
|
||||
SetDataObject(ido, true);
|
||||
}
|
||||
|
||||
private static string getHTMLString(ISurface surface, string filename) {
|
||||
|
@ -404,8 +450,8 @@ EndSelection:<<<<<<<4
|
|||
|
||||
/// <summary>
|
||||
/// Set an Image to the clipboard
|
||||
/// This method will place 2 images to the clipboard, one of type Bitmap which
|
||||
/// works with pretty much everything and one of type Dib for e.g. OpenOffice
|
||||
/// This method will place images to the clipboard depending on the ClipboardFormats setting.
|
||||
/// e.g. Bitmap which works with pretty much everything and type Dib for e.g. OpenOffice
|
||||
/// because OpenOffice has a bug http://qa.openoffice.org/issues/show_bug.cgi?id=85661
|
||||
/// The Dib (Device Indenpendend Bitmap) in 32bpp actually won't work with Powerpoint 2003!
|
||||
/// When pasting a Dib in PP 2003 the Bitmap is somehow shifted left!
|
||||
|
@ -430,7 +476,7 @@ EndSelection:<<<<<<<4
|
|||
ImageOutput.SaveToStream(surface, pngStream, pngOutputSettings);
|
||||
pngStream.Seek(0, SeekOrigin.Begin);
|
||||
// Set the PNG stream
|
||||
ido.SetData("PNG", false, pngStream);
|
||||
ido.SetData(FORMAT_PNG, false, pngStream);
|
||||
}
|
||||
} catch (Exception pngEX) {
|
||||
LOG.Error("Error creating PNG for the Clipboard.", pngEX);
|
||||
|
@ -475,8 +521,17 @@ EndSelection:<<<<<<<4
|
|||
}
|
||||
} finally {
|
||||
// we need to use the SetDataOject before the streams are closed otherwise the buffer will be gone!
|
||||
// Check if Bitmap is wanted
|
||||
if (config.ClipboardFormats.Contains(ClipboardFormat.BITMAP)) {
|
||||
using (Image tmpImage = surface.GetImageForExport()) {
|
||||
ido.SetImage(tmpImage);
|
||||
// Place the DataObject to the clipboard
|
||||
SetDataObject(ido);
|
||||
SetDataObject(ido, true);
|
||||
}
|
||||
} else {
|
||||
// Place the DataObject to the clipboard
|
||||
SetDataObject(ido, true);
|
||||
}
|
||||
|
||||
if (pngStream != null) {
|
||||
pngStream.Dispose();
|
||||
|
@ -608,16 +663,18 @@ EndSelection:<<<<<<<4
|
|||
/// <returns></returns>
|
||||
public static List<string> GetImageFilenames(IDataObject dataObject) {
|
||||
List<string> filenames = new List<string>();
|
||||
string[] dropFileNames = (string[])dataObject.GetData(DataFormats.FileDrop);
|
||||
string[] dropFileNames = (string[]) dataObject.GetData(DataFormats.FileDrop);
|
||||
try {
|
||||
if (dropFileNames != null && dropFileNames.Length > 0) {
|
||||
foreach (string filename in dropFileNames) {
|
||||
LOG.Debug("Found filename: " + filename);
|
||||
string ext = Path.GetExtension(filename).ToLower();
|
||||
if ((ext == ".jpg") || (ext == ".jpeg") || (ext == ".tiff") || (ext == ".gif") || (ext == ".png") || (ext == ".bmp") || (ext == ".ico") || (ext == ".wmf")) {
|
||||
filenames.Add(filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
}
|
||||
return filenames;
|
||||
}
|
||||
|
||||
|
|
|
@ -28,7 +28,7 @@ using Greenshot.Plugin;
|
|||
|
||||
namespace GreenshotPlugin.Core {
|
||||
public enum ClipboardFormat {
|
||||
PNG, DIB, HTML, HTMLDATAURL
|
||||
PNG, DIB, HTML, HTMLDATAURL, BITMAP
|
||||
}
|
||||
public enum OutputFormat {
|
||||
bmp, gif, jpg, png, tiff, greenshot
|
||||
|
|
|
@ -33,7 +33,7 @@ namespace Greenshot.Core {
|
|||
/// Interface to describe an effect
|
||||
/// </summary>
|
||||
public interface IEffect {
|
||||
Bitmap Apply(Bitmap sourceBitmap, out Point offsetChange);
|
||||
Image Apply(Image sourceImage, out Point offsetChange);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -57,8 +57,8 @@ namespace Greenshot.Core {
|
|||
get;
|
||||
set;
|
||||
}
|
||||
public virtual Bitmap Apply(Bitmap sourceBitmap, out Point offsetChange) {
|
||||
return ImageHelper.CreateShadow(sourceBitmap, Darkness, ShadowSize, ShadowOffset, out offsetChange, PixelFormat.Format32bppArgb); //Image.PixelFormat);
|
||||
public virtual Image Apply(Image sourceImage, out Point offsetChange) {
|
||||
return ImageHelper.CreateShadow(sourceImage, Darkness, ShadowSize, ShadowOffset, out offsetChange, PixelFormat.Format32bppArgb); //Image.PixelFormat);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -84,8 +84,8 @@ namespace Greenshot.Core {
|
|||
get;
|
||||
set;
|
||||
}
|
||||
public override Bitmap Apply(Bitmap sourceBitmap, out Point offsetChange) {
|
||||
using (Bitmap tmpTornImage = ImageHelper.CreateTornEdge(sourceBitmap, ToothHeight, HorizontalToothRange, VerticalToothRange)) {
|
||||
public override Image Apply(Image sourceImage, out Point offsetChange) {
|
||||
using (Image tmpTornImage = ImageHelper.CreateTornEdge(sourceImage, ToothHeight, HorizontalToothRange, VerticalToothRange)) {
|
||||
return ImageHelper.CreateShadow(tmpTornImage, Darkness, ShadowSize, ShadowOffset, out offsetChange, PixelFormat.Format32bppArgb);
|
||||
}
|
||||
}
|
||||
|
@ -95,9 +95,9 @@ namespace Greenshot.Core {
|
|||
/// GrayscaleEffect
|
||||
/// </summary>
|
||||
public class GrayscaleEffect : IEffect {
|
||||
public Bitmap Apply(Bitmap sourceBitmap, out Point offsetChange) {
|
||||
public Image Apply(Image sourceImage, out Point offsetChange) {
|
||||
offsetChange = Point.Empty;
|
||||
return ImageHelper.CreateGrayscale(sourceBitmap);
|
||||
return ImageHelper.CreateGrayscale(sourceImage);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -105,9 +105,9 @@ namespace Greenshot.Core {
|
|||
/// InvertEffect
|
||||
/// </summary>
|
||||
public class InvertEffect : IEffect {
|
||||
public Bitmap Apply(Bitmap sourceBitmap, out Point offsetChange) {
|
||||
public Image Apply(Image sourceImage, out Point offsetChange) {
|
||||
offsetChange = Point.Empty;
|
||||
return ImageHelper.CreateNegative(sourceBitmap);
|
||||
return ImageHelper.CreateNegative(sourceImage);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -127,8 +127,8 @@ namespace Greenshot.Core {
|
|||
get;
|
||||
set;
|
||||
}
|
||||
public Bitmap Apply(Bitmap sourceBitmap, out Point offsetChange) {
|
||||
return ImageHelper.CreateBorder(sourceBitmap, Width, Color, sourceBitmap.PixelFormat, out offsetChange);
|
||||
public Image Apply(Image sourceImage, out Point offsetChange) {
|
||||
return ImageHelper.CreateBorder(sourceImage, Width, Color, sourceImage.PixelFormat, out offsetChange);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -143,7 +143,7 @@ namespace Greenshot.Core {
|
|||
get;
|
||||
set;
|
||||
}
|
||||
public Bitmap Apply(Bitmap sourceBitmap, out Point offsetChange) {
|
||||
public Image Apply(Image sourceImage, out Point offsetChange) {
|
||||
offsetChange = Point.Empty;
|
||||
RotateFlipType flipType;
|
||||
if (Angle == 90) {
|
||||
|
@ -153,7 +153,7 @@ namespace Greenshot.Core {
|
|||
} else {
|
||||
throw new NotSupportedException("Currently only an angle of 90 or -90 (270) is supported.");
|
||||
}
|
||||
return ImageHelper.RotateFlip(sourceBitmap, flipType);
|
||||
return ImageHelper.RotateFlip(sourceImage, flipType);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -178,9 +178,9 @@ namespace Greenshot.Core {
|
|||
get;
|
||||
set;
|
||||
}
|
||||
public Bitmap Apply(Bitmap sourceBitmap, out Point offsetChange) {
|
||||
public Image Apply(Image sourceImage, out Point offsetChange) {
|
||||
offsetChange = Point.Empty;
|
||||
return ImageHelper.ResizeBitmap(sourceBitmap, MaintainAspectRatio, Width, Height);
|
||||
return ImageHelper.ResizeImage(sourceImage, MaintainAspectRatio, Width, Height);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -215,10 +215,10 @@ namespace Greenshot.Core {
|
|||
get;
|
||||
set;
|
||||
}
|
||||
public Bitmap Apply(Bitmap sourceBitmap, out Point offsetChange) {
|
||||
public Image Apply(Image sourceImage, out Point offsetChange) {
|
||||
// Make sure the elements move according to the offset the effect made the bitmap move
|
||||
offsetChange = new Point(Left, Top);
|
||||
return ImageHelper.ResizeCanvas(sourceBitmap, BackgroundColor, Left, Right, Top, Bottom);
|
||||
return ImageHelper.ResizeCanvas(sourceImage, BackgroundColor, Left, Right, Top, Bottom);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -193,11 +193,14 @@ namespace GreenshotPlugin.Core {
|
|||
/// </summary>
|
||||
/// <param name="filename"></param>
|
||||
/// <returns></returns>
|
||||
public static Bitmap LoadBitmap(string filename) {
|
||||
public static Image LoadImage(string filename) {
|
||||
if (string.IsNullOrEmpty(filename)) {
|
||||
return null;
|
||||
}
|
||||
Bitmap fileBitmap = null;
|
||||
if (!File.Exists(filename)) {
|
||||
return null;
|
||||
}
|
||||
Image fileImage = null;
|
||||
LOG.InfoFormat("Loading image from file {0}", filename);
|
||||
// Fixed lock problem Bug #3431881
|
||||
using (Stream imageFileStream = File.OpenRead(filename)) {
|
||||
|
@ -209,20 +212,20 @@ namespace GreenshotPlugin.Core {
|
|||
try {
|
||||
using (Image tmpImage = ExtractVistaIcon(imageFileStream)) {
|
||||
if (tmpImage != null) {
|
||||
fileBitmap = Clone(tmpImage);
|
||||
fileImage = Clone(tmpImage);
|
||||
}
|
||||
}
|
||||
} catch (Exception vistaIconException) {
|
||||
LOG.Warn("Can't read icon from " + filename, vistaIconException);
|
||||
}
|
||||
if (fileBitmap == null) {
|
||||
if (fileImage == null) {
|
||||
try {
|
||||
// No vista icon, try normal icon
|
||||
imageFileStream.Position = 0;
|
||||
// We create a copy of the bitmap, so everything else can be disposed
|
||||
using (Icon tmpIcon = new Icon(imageFileStream, new Size(1024,1024))) {
|
||||
using (Image tmpImage = tmpIcon.ToBitmap()) {
|
||||
fileBitmap = Clone(tmpImage);
|
||||
fileImage = Clone(tmpImage);
|
||||
}
|
||||
}
|
||||
} catch (Exception iconException) {
|
||||
|
@ -230,19 +233,19 @@ namespace GreenshotPlugin.Core {
|
|||
}
|
||||
}
|
||||
}
|
||||
if (fileBitmap == null) {
|
||||
if (fileImage == null) {
|
||||
// We create a copy of the bitmap, so everything else can be disposed
|
||||
imageFileStream.Position = 0;
|
||||
using (Image tmpImage = Image.FromStream(imageFileStream, true, true)) {
|
||||
LOG.DebugFormat("Loaded {0} with Size {1}x{2} and PixelFormat {3}", filename, tmpImage.Width, tmpImage.Height, tmpImage.PixelFormat);
|
||||
fileBitmap = Clone(tmpImage);
|
||||
fileImage = Clone(tmpImage);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (fileBitmap != null) {
|
||||
LOG.InfoFormat("Information about file {0}: {1}x{2}-{3} Resolution {4}x{5}", filename, fileBitmap.Width, fileBitmap.Height, fileBitmap.PixelFormat, fileBitmap.HorizontalResolution, fileBitmap.VerticalResolution);
|
||||
if (fileImage != null) {
|
||||
LOG.InfoFormat("Information about file {0}: {1}x{2}-{3} Resolution {4}x{5}", filename, fileImage.Width, fileImage.Height, fileImage.PixelFormat, fileImage.HorizontalResolution, fileImage.VerticalResolution);
|
||||
}
|
||||
return fileBitmap;
|
||||
return fileImage;
|
||||
}
|
||||
|
||||
|
||||
|
@ -336,10 +339,10 @@ namespace GreenshotPlugin.Core {
|
|||
/// <param name="sourceBitmap">Bitmap</param>
|
||||
/// <param name="effect">IEffect</param>
|
||||
/// <returns>Bitmap</returns>
|
||||
public static Bitmap ApplyEffect(Bitmap sourceBitmap, IEffect effect, out Point offset) {
|
||||
public static Image ApplyEffect(Image sourceImage, IEffect effect, out Point offset) {
|
||||
List<IEffect> effects = new List<IEffect>();
|
||||
effects.Add(effect);
|
||||
return ApplyEffects(sourceBitmap, effects, out offset);
|
||||
return ApplyEffects(sourceImage, effects, out offset);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -348,40 +351,40 @@ namespace GreenshotPlugin.Core {
|
|||
/// <param name="sourceBitmap">Bitmap</param>
|
||||
/// <param name="effects">List<IEffect></param>
|
||||
/// <returns>Bitmap</returns>
|
||||
public static Bitmap ApplyEffects(Bitmap sourceBitmap, List<IEffect> effects, out Point offset) {
|
||||
Bitmap currentBitmap = sourceBitmap;
|
||||
public static Image ApplyEffects(Image sourceImage, List<IEffect> effects, out Point offset) {
|
||||
Image currentImage = sourceImage;
|
||||
bool disposeImage = false;
|
||||
// Default out value for the offset, will be modified there where needed
|
||||
offset = new Point(0, 0);
|
||||
Point tmpPoint;
|
||||
Bitmap tmpBitmap = null;
|
||||
Image tmpImage = null;
|
||||
foreach (IEffect effect in effects) {
|
||||
tmpBitmap = effect.Apply(currentBitmap, out tmpPoint);
|
||||
tmpImage = effect.Apply(currentImage, out tmpPoint);
|
||||
offset.Offset(tmpPoint);
|
||||
if (disposeImage) {
|
||||
currentBitmap.Dispose();
|
||||
currentImage.Dispose();
|
||||
}
|
||||
currentBitmap = tmpBitmap;
|
||||
currentImage = tmpImage;
|
||||
// Make sure the "new" image is disposed
|
||||
disposeImage = true;
|
||||
}
|
||||
return tmpBitmap;
|
||||
return tmpImage;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Make the picture look like it's torn
|
||||
/// </summary>
|
||||
/// <param name="sourceBitmap">Bitmap to make torn edge off</param>
|
||||
/// <param name="sourceImage">Bitmap to make torn edge off</param>
|
||||
/// <param name="toothHeight">How large (height) is each tooth</param>
|
||||
/// <param name="horizontalToothRange">How wide is a horizontal tooth</param>
|
||||
/// <param name="verticalToothRange">How wide is a vertical tooth</param>
|
||||
/// <returns>Changed bitmap</returns>
|
||||
public static Bitmap CreateTornEdge(Bitmap sourceBitmap, int toothHeight, int horizontalToothRange, int verticalToothRange) {
|
||||
Bitmap returnImage = CreateEmpty(sourceBitmap.Width, sourceBitmap.Height, PixelFormat.Format32bppArgb, Color.Empty, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution);
|
||||
public static Image CreateTornEdge(Image sourceImage, int toothHeight, int horizontalToothRange, int verticalToothRange) {
|
||||
Image returnImage = CreateEmpty(sourceImage.Width, sourceImage.Height, PixelFormat.Format32bppArgb, Color.Empty, sourceImage.HorizontalResolution, sourceImage.VerticalResolution);
|
||||
using (GraphicsPath path = new GraphicsPath()) {
|
||||
Random random = new Random();
|
||||
int HorizontalRegions = (int)(sourceBitmap.Width / horizontalToothRange);
|
||||
int VerticalRegions = (int)(sourceBitmap.Height / verticalToothRange);
|
||||
int HorizontalRegions = (int)(sourceImage.Width / horizontalToothRange);
|
||||
int VerticalRegions = (int)(sourceImage.Height / verticalToothRange);
|
||||
|
||||
// Start
|
||||
Point previousEndingPoint = new Point(horizontalToothRange, random.Next(1, toothHeight));
|
||||
|
@ -397,7 +400,7 @@ namespace GreenshotPlugin.Core {
|
|||
|
||||
// Right
|
||||
for (int i = 0; i < VerticalRegions; i++) {
|
||||
int x = sourceBitmap.Width - random.Next(1, toothHeight);
|
||||
int x = sourceImage.Width - random.Next(1, toothHeight);
|
||||
int y = (int)previousEndingPoint.Y + verticalToothRange;
|
||||
newEndingPoint = new Point(x, y);
|
||||
path.AddLine(previousEndingPoint, newEndingPoint);
|
||||
|
@ -407,7 +410,7 @@ namespace GreenshotPlugin.Core {
|
|||
// Bottom
|
||||
for (int i = 0; i < HorizontalRegions; i++) {
|
||||
int x = (int)previousEndingPoint.X - horizontalToothRange;
|
||||
int y = sourceBitmap.Height - random.Next(1, toothHeight);
|
||||
int y = sourceImage.Height - random.Next(1, toothHeight);
|
||||
newEndingPoint = new Point(x, y);
|
||||
path.AddLine(previousEndingPoint, newEndingPoint);
|
||||
previousEndingPoint = newEndingPoint;
|
||||
|
@ -429,7 +432,7 @@ namespace GreenshotPlugin.Core {
|
|||
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
|
||||
graphics.CompositingQuality = CompositingQuality.HighQuality;
|
||||
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
||||
using (Brush brush = new TextureBrush(sourceBitmap)) {
|
||||
using (Brush brush = new TextureBrush(sourceImage)) {
|
||||
// Imporant note: If the target wouldn't be at 0,0 we need to translate-transform!!
|
||||
graphics.FillPath(brush, path);
|
||||
}
|
||||
|
@ -948,10 +951,10 @@ namespace GreenshotPlugin.Core {
|
|||
/// <summary>
|
||||
/// Return negative of Bitmap
|
||||
/// </summary>
|
||||
/// <param name="sourceBitmap">Bitmap to create a negative off</param>
|
||||
/// <param name="sourceImage">Bitmap to create a negative off</param>
|
||||
/// <returns>Negative bitmap</returns>
|
||||
public static Bitmap CreateNegative(Bitmap sourceBitmap) {
|
||||
using (BitmapBuffer bb = new BitmapBuffer(sourceBitmap, true)) {
|
||||
public static Bitmap CreateNegative(Image sourceImage) {
|
||||
using (BitmapBuffer bb = new BitmapBuffer(sourceImage, true)) {
|
||||
bb.Lock();
|
||||
for (int y = 0; y < bb.Height; y++) {
|
||||
for (int x = 0; x < bb.Width; x++) {
|
||||
|
@ -968,18 +971,18 @@ namespace GreenshotPlugin.Core {
|
|||
/// <summary>
|
||||
/// Create a new bitmap where the sourceBitmap has a Simple border around it
|
||||
/// </summary>
|
||||
/// <param name="sourceBitmap">Bitmap to make a border on</param>
|
||||
/// <param name="sourceImage">Bitmap to make a border on</param>
|
||||
/// <param name="borderSize">Size of the border</param>
|
||||
/// <param name="borderColor">Color of the border</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>
|
||||
/// <returns>Bitmap with the shadow, is bigger than the sourceBitmap!!</returns>
|
||||
public static Bitmap CreateBorder(Bitmap sourceBitmap, int borderSize, Color borderColor, PixelFormat targetPixelformat, out Point offset) {
|
||||
public static Image CreateBorder(Image sourceImage, int borderSize, Color borderColor, PixelFormat targetPixelformat, out Point offset) {
|
||||
// "return" the shifted offset, so the caller can e.g. move elements
|
||||
offset = new Point(borderSize, borderSize);
|
||||
|
||||
// Create a new "clean" image
|
||||
Bitmap newImage = CreateEmpty(sourceBitmap.Width + (borderSize * 2), sourceBitmap.Height + (borderSize * 2), targetPixelformat, Color.Empty, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution);
|
||||
Bitmap newImage = CreateEmpty(sourceImage.Width + (borderSize * 2), sourceImage.Height + (borderSize * 2), targetPixelformat, Color.Empty, sourceImage.HorizontalResolution, sourceImage.VerticalResolution);
|
||||
using (Graphics graphics = Graphics.FromImage(newImage)) {
|
||||
// Make sure we draw with the best quality!
|
||||
graphics.SmoothingMode = SmoothingMode.HighQuality;
|
||||
|
@ -996,10 +999,10 @@ namespace GreenshotPlugin.Core {
|
|||
}
|
||||
}
|
||||
// draw original with a TextureBrush so we have nice antialiasing!
|
||||
using (Brush textureBrush = new TextureBrush(sourceBitmap, WrapMode.Clamp)) {
|
||||
using (Brush textureBrush = new TextureBrush(sourceImage, 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);
|
||||
graphics.FillRectangle(textureBrush, 0, 0, sourceImage.Width, sourceImage.Height);
|
||||
}
|
||||
}
|
||||
return newImage;
|
||||
|
@ -1008,12 +1011,12 @@ namespace GreenshotPlugin.Core {
|
|||
/// <summary>
|
||||
/// Create a new bitmap where the sourceBitmap is in grayscale
|
||||
/// </summary>
|
||||
/// <param name="sourceBitmap">Original bitmap</param>
|
||||
/// <param name="sourceImage">Original bitmap</param>
|
||||
/// <returns>Bitmap with grayscale</returns>
|
||||
public static Bitmap CreateGrayscale(Bitmap sourceBitmap) {
|
||||
public static Image CreateGrayscale(Image sourceImage) {
|
||||
//create a blank bitmap the same size as original
|
||||
// If using 8bpp than the following exception comes: A Graphics object cannot be created from an image that has an indexed pixel format.
|
||||
Bitmap newBitmap = CreateEmpty(sourceBitmap.Width, sourceBitmap.Height, PixelFormat.Format24bppRgb, Color.Empty, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution);
|
||||
Bitmap newBitmap = CreateEmpty(sourceImage.Width, sourceImage.Height, PixelFormat.Format24bppRgb, Color.Empty, sourceImage.HorizontalResolution, sourceImage.VerticalResolution);
|
||||
//ColorPalette imagePalette = newBitmap.Palette;
|
||||
//for (int i = 0; i <= 255; i++) {
|
||||
// // create greyscale color table
|
||||
|
@ -1040,7 +1043,7 @@ namespace GreenshotPlugin.Core {
|
|||
attributes.SetColorMatrix(colorMatrix);
|
||||
|
||||
//draw the original image on the new image using the grayscale color matrix
|
||||
graphics.DrawImage(sourceBitmap, new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height), 0, 0, sourceBitmap.Width, sourceBitmap.Height, GraphicsUnit.Pixel, attributes);
|
||||
graphics.DrawImage(sourceImage, new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), 0, 0, sourceImage.Width, sourceImage.Height, GraphicsUnit.Pixel, attributes);
|
||||
}
|
||||
|
||||
return newBitmap;
|
||||
|
@ -1049,10 +1052,10 @@ namespace GreenshotPlugin.Core {
|
|||
/// <summary>
|
||||
/// Checks if the supplied Bitmap has a PixelFormat we support
|
||||
/// </summary>
|
||||
/// <param name="bitmap">bitmap to check</param>
|
||||
/// <param name="image">bitmap to check</param>
|
||||
/// <returns>bool if we support it</returns>
|
||||
public static bool SupportsPixelFormat(Bitmap bitmap) {
|
||||
return SupportsPixelFormat(bitmap.PixelFormat);
|
||||
public static bool SupportsPixelFormat(Image image) {
|
||||
return SupportsPixelFormat(image.PixelFormat);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -1070,11 +1073,15 @@ namespace GreenshotPlugin.Core {
|
|||
/// <summary>
|
||||
/// Wrapper for just cloning which calls the CloneArea
|
||||
/// </summary>
|
||||
/// <param name="sourceBitmap">Image to clone</param>
|
||||
/// <param name="sourceImage">Image to clone</param>
|
||||
/// <returns>Bitmap with clone image data</returns>
|
||||
public static Bitmap Clone(Image sourceBitmap) {
|
||||
return CloneArea(sourceBitmap, Rectangle.Empty, PixelFormat.DontCare);
|
||||
public static Image Clone(Image sourceImage) {
|
||||
if (sourceImage is Metafile) {
|
||||
return (Image)sourceImage.Clone();
|
||||
}
|
||||
return CloneArea(sourceImage, Rectangle.Empty, PixelFormat.DontCare);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wrapper for just cloning & TargetFormat which calls the CloneArea
|
||||
/// </summary>
|
||||
|
@ -1092,24 +1099,24 @@ namespace GreenshotPlugin.Core {
|
|||
/// a quick workaround is using new Bitmap which uses a default of Format32bppArgb
|
||||
/// 2) When going from a transparent to a non transparent bitmap, we draw the background white!
|
||||
/// </summary>
|
||||
/// <param name="sourceBitmap">Source bitmap to clone</param>
|
||||
/// <param name="sourceImage">Source bitmap to clone</param>
|
||||
/// <param name="sourceRect">Rectangle to copy from the source, use Rectangle.Empty for all</param>
|
||||
/// <param name="targetFormat">Target Format, use PixelFormat.DontCare if you want the original (or a default if the source PixelFormat is not supported)</param>
|
||||
/// <returns></returns>
|
||||
public static Bitmap CloneArea(Image sourceBitmap, Rectangle sourceRect, PixelFormat targetFormat) {
|
||||
public static Bitmap CloneArea(Image sourceImage, Rectangle sourceRect, PixelFormat targetFormat) {
|
||||
Bitmap newImage = null;
|
||||
Rectangle bitmapRect = new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height);
|
||||
Rectangle bitmapRect = new Rectangle(0, 0, sourceImage.Width, sourceImage.Height);
|
||||
|
||||
// Make sure the source is not Rectangle.Empty
|
||||
if (Rectangle.Empty.Equals(sourceRect)) {
|
||||
sourceRect = new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height);
|
||||
sourceRect = new Rectangle(0, 0, sourceImage.Width, sourceImage.Height);
|
||||
}
|
||||
|
||||
// If no pixelformat is supplied
|
||||
if (PixelFormat.DontCare == targetFormat || PixelFormat.Undefined == targetFormat) {
|
||||
if (SupportsPixelFormat(sourceBitmap.PixelFormat)) {
|
||||
targetFormat = sourceBitmap.PixelFormat;
|
||||
} else if (Image.IsAlphaPixelFormat(sourceBitmap.PixelFormat)) {
|
||||
if (SupportsPixelFormat(sourceImage.PixelFormat)) {
|
||||
targetFormat = sourceImage.PixelFormat;
|
||||
} else if (Image.IsAlphaPixelFormat(sourceImage.PixelFormat)) {
|
||||
targetFormat = PixelFormat.Format32bppArgb;
|
||||
} else {
|
||||
targetFormat = PixelFormat.Format24bppRgb;
|
||||
|
@ -1126,15 +1133,15 @@ namespace GreenshotPlugin.Core {
|
|||
}
|
||||
|
||||
bool destinationIsTransparent = Image.IsAlphaPixelFormat(targetFormat);
|
||||
bool sourceIsTransparent = Image.IsAlphaPixelFormat(sourceBitmap.PixelFormat);
|
||||
bool sourceIsTransparent = Image.IsAlphaPixelFormat(sourceImage.PixelFormat);
|
||||
bool fromTransparentToNon = !destinationIsTransparent && sourceIsTransparent;
|
||||
bool isBitmap = sourceBitmap is Bitmap;
|
||||
bool isBitmap = sourceImage is Bitmap;
|
||||
bool isAreaEqual = sourceRect.Equals(bitmapRect);
|
||||
if (isAreaEqual || fromTransparentToNon || !isBitmap) {
|
||||
// Rule 1: if the areas are equal, always copy ourselves
|
||||
newImage = new Bitmap(bitmapRect.Width, bitmapRect.Height, targetFormat);
|
||||
// Make sure both images have the same resolution
|
||||
newImage.SetResolution(sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution);
|
||||
newImage.SetResolution(sourceImage.HorizontalResolution, sourceImage.VerticalResolution);
|
||||
|
||||
using (Graphics graphics = Graphics.FromImage(newImage)) {
|
||||
if (fromTransparentToNon) {
|
||||
|
@ -1143,16 +1150,16 @@ namespace GreenshotPlugin.Core {
|
|||
}
|
||||
// decide fastest copy method
|
||||
if (isAreaEqual) {
|
||||
graphics.DrawImageUnscaled(sourceBitmap, 0, 0);
|
||||
graphics.DrawImageUnscaled(sourceImage, 0, 0);
|
||||
} else {
|
||||
graphics.DrawImage(sourceBitmap, 0, 0, sourceRect, GraphicsUnit.Pixel);
|
||||
graphics.DrawImage(sourceImage, 0, 0, sourceRect, GraphicsUnit.Pixel);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Let GDI+ decide how to convert, need to test what is quicker...
|
||||
newImage = (sourceBitmap as Bitmap).Clone(sourceRect, targetFormat);
|
||||
newImage = (sourceImage as Bitmap).Clone(sourceRect, targetFormat);
|
||||
// Make sure both images have the same resolution
|
||||
newImage.SetResolution(sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution);
|
||||
newImage.SetResolution(sourceImage.HorizontalResolution, sourceImage.VerticalResolution);
|
||||
}
|
||||
return newImage;
|
||||
}
|
||||
|
@ -1160,27 +1167,27 @@ namespace GreenshotPlugin.Core {
|
|||
/// <summary>
|
||||
/// Rotate the bitmap
|
||||
/// </summary>
|
||||
/// <param name="sourceBitmap"></param>
|
||||
/// <param name="sourceImage"></param>
|
||||
/// <param name="rotateFlipType"></param>
|
||||
/// <returns></returns>
|
||||
public static Bitmap RotateFlip(Bitmap sourceBitmap, RotateFlipType rotateFlipType) {
|
||||
Bitmap returnBitmap = Clone(sourceBitmap);
|
||||
returnBitmap.RotateFlip(rotateFlipType);
|
||||
return returnBitmap;
|
||||
public static Image RotateFlip(Image sourceImage, RotateFlipType rotateFlipType) {
|
||||
Image returnImage = Clone(sourceImage);
|
||||
returnImage.RotateFlip(rotateFlipType);
|
||||
return returnImage;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A generic way to create an empty image
|
||||
/// </summary>
|
||||
/// <param name="sourceBitmap">the source bitmap as the specifications for the new bitmap</param>
|
||||
/// <param name="sourceImage">the source bitmap as the specifications for the new bitmap</param>
|
||||
/// <param name="backgroundColor">The color to fill with, or Color.Empty to take the default depending on the pixel format</param>
|
||||
/// <returns></returns>
|
||||
public static Bitmap CreateEmptyLike(Bitmap sourceBitmap, Color backgroundColor) {
|
||||
PixelFormat pixelFormat = sourceBitmap.PixelFormat;
|
||||
public static Bitmap CreateEmptyLike(Image sourceImage, Color backgroundColor) {
|
||||
PixelFormat pixelFormat = sourceImage.PixelFormat;
|
||||
if (backgroundColor.A < 255) {
|
||||
pixelFormat = PixelFormat.Format32bppArgb;
|
||||
}
|
||||
return CreateEmpty(sourceBitmap.Width, sourceBitmap.Height, pixelFormat, backgroundColor, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution);
|
||||
return CreateEmpty(sourceImage.Width, sourceImage.Height, pixelFormat, backgroundColor, sourceImage.HorizontalResolution, sourceImage.VerticalResolution);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -1236,17 +1243,17 @@ namespace GreenshotPlugin.Core {
|
|||
/// <summary>
|
||||
/// Resize canvas with pixel to the left, right, top and bottom
|
||||
/// </summary>
|
||||
/// <param name="sourceBitmap"></param>
|
||||
/// <param name="sourceImage"></param>
|
||||
/// <param name="backgroundColor">The color to fill with, or Color.Empty to take the default depending on the pixel format</param>
|
||||
/// <param name="left"></param>
|
||||
/// <param name="right"></param>
|
||||
/// <param name="top"></param>
|
||||
/// <param name="bottom"></param>
|
||||
/// <returns>a new bitmap with the source copied on it</returns>
|
||||
public static Bitmap ResizeCanvas(Bitmap sourceBitmap, Color backgroundColor, int left, int right, int top, int bottom) {
|
||||
Bitmap newBitmap = CreateEmpty(sourceBitmap.Width + left + right, sourceBitmap.Height + top + bottom, sourceBitmap.PixelFormat, backgroundColor, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution);
|
||||
public static Image ResizeCanvas(Image sourceImage, Color backgroundColor, int left, int right, int top, int bottom) {
|
||||
Bitmap newBitmap = CreateEmpty(sourceImage.Width + left + right, sourceImage.Height + top + bottom, sourceImage.PixelFormat, backgroundColor, sourceImage.HorizontalResolution, sourceImage.VerticalResolution);
|
||||
using (Graphics graphics = Graphics.FromImage(newBitmap)) {
|
||||
graphics.DrawImageUnscaled(sourceBitmap, left, top);
|
||||
graphics.DrawImageUnscaled(sourceImage, left, top);
|
||||
}
|
||||
return newBitmap;
|
||||
}
|
||||
|
@ -1254,30 +1261,30 @@ namespace GreenshotPlugin.Core {
|
|||
/// <summary>
|
||||
/// Wrapper for the more complex Resize, this resize could be used for e.g. Thumbnails
|
||||
/// </summary>
|
||||
/// <param name="sourceBitmap"></param>
|
||||
/// <param name="sourceImage"></param>
|
||||
/// <param name="maintainAspectRatio">true to maintain the aspect ratio</param>
|
||||
/// <param name="newWidth"></param>
|
||||
/// <param name="newHeight"></param>
|
||||
/// <returns></returns>
|
||||
public static Bitmap ResizeBitmap(Bitmap sourceBitmap, bool maintainAspectRatio, int newWidth, int newHeight) {
|
||||
public static Image ResizeImage(Image sourceImage, bool maintainAspectRatio, int newWidth, int newHeight) {
|
||||
Point throwAway;
|
||||
return ResizeBitmap(sourceBitmap, maintainAspectRatio, false, Color.Empty, newWidth, newHeight, out throwAway);
|
||||
return ResizeImage(sourceImage, maintainAspectRatio, false, Color.Empty, newWidth, newHeight, out throwAway);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Count how many times the supplied color exists
|
||||
/// </summary>
|
||||
/// <param name="sourceBitmap">Bitmap to count the pixels of</param>
|
||||
/// <param name="sourceImage">Image to count the pixels of</param>
|
||||
/// <param name="colorToCount">Color to count/param>
|
||||
/// <param name="includeAlpha">true if Alpha needs to be checked</param>
|
||||
/// <returns>int with the number of pixels which have colorToCount</returns>
|
||||
public static int CountColor(Bitmap sourceBitmap, Color colorToCount, bool includeAlpha) {
|
||||
public static int CountColor(Image sourceImage, Color colorToCount, bool includeAlpha) {
|
||||
int colors = 0;
|
||||
int toCount = colorToCount.ToArgb();
|
||||
if (!includeAlpha) {
|
||||
toCount = toCount & 0xffffff;
|
||||
}
|
||||
using (BitmapBuffer bb = new BitmapBuffer(sourceBitmap, true)) {
|
||||
using (BitmapBuffer bb = new BitmapBuffer(sourceImage)) {
|
||||
bb.Lock();
|
||||
for (int y = 0; y < bb.Height; y++) {
|
||||
for (int x = 0; x < bb.Width; x++) {
|
||||
|
@ -1298,57 +1305,57 @@ namespace GreenshotPlugin.Core {
|
|||
/// <summary>
|
||||
/// Scale the bitmap, keeping aspect ratio, but the canvas will always have the specified size.
|
||||
/// </summary>
|
||||
/// <param name="sourceBitmap">Bitmap to scale</param>
|
||||
/// <param name="sourceImage">Image to scale</param>
|
||||
/// <param name="maintainAspectRatio">true to maintain the aspect ratio</param>
|
||||
/// <param name="backgroundColor">The color to fill with, or Color.Empty to take the default depending on the pixel format</param>
|
||||
/// <param name="newWidth">new width</param>
|
||||
/// <param name="newHeight">new height</param>
|
||||
/// <returns>a new bitmap with the specified size, the source-bitmap scaled to fit with aspect ratio locked</returns>
|
||||
public static Bitmap ResizeBitmap(Bitmap sourceBitmap, bool maintainAspectRatio, bool canvasUseNewSize, Color backgroundColor, int newWidth, int newHeight, out Point offset) {
|
||||
/// <returns>a new bitmap with the specified size, the source-Image scaled to fit with aspect ratio locked</returns>
|
||||
public static Image ResizeImage(Image sourceImage, bool maintainAspectRatio, bool canvasUseNewSize, Color backgroundColor, int newWidth, int newHeight, out Point offset) {
|
||||
int destX = 0;
|
||||
int destY = 0;
|
||||
|
||||
float nPercentW = 0;
|
||||
float nPercentH = 0;
|
||||
|
||||
nPercentW = ((float)newWidth / (float)sourceBitmap.Width);
|
||||
nPercentH = ((float)newHeight / (float)sourceBitmap.Height);
|
||||
nPercentW = ((float)newWidth / (float)sourceImage.Width);
|
||||
nPercentH = ((float)newHeight / (float)sourceImage.Height);
|
||||
if (maintainAspectRatio) {
|
||||
if (nPercentH != 0 && nPercentH < nPercentW) {
|
||||
nPercentW = nPercentH;
|
||||
if (canvasUseNewSize) {
|
||||
destX = Math.Max(0, System.Convert.ToInt32((newWidth - (sourceBitmap.Width * nPercentW)) / 2));
|
||||
destX = Math.Max(0, System.Convert.ToInt32((newWidth - (sourceImage.Width * nPercentW)) / 2));
|
||||
}
|
||||
} else {
|
||||
nPercentH = nPercentW;
|
||||
if (canvasUseNewSize) {
|
||||
destY = Math.Max(0, System.Convert.ToInt32((newHeight - (sourceBitmap.Height * nPercentH)) / 2));
|
||||
destY = Math.Max(0, System.Convert.ToInt32((newHeight - (sourceImage.Height * nPercentH)) / 2));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
offset = new Point(destX, destY);
|
||||
|
||||
int destWidth = (int)(sourceBitmap.Width * nPercentW);
|
||||
int destHeight = (int)(sourceBitmap.Height * nPercentH);
|
||||
int destWidth = (int)(sourceImage.Width * nPercentW);
|
||||
int destHeight = (int)(sourceImage.Height * nPercentH);
|
||||
if (newWidth == 0) {
|
||||
newWidth = destWidth;
|
||||
}
|
||||
if (newHeight == 0) {
|
||||
newHeight = destHeight;
|
||||
}
|
||||
Bitmap newBitmap = null;
|
||||
Image newImage = null;
|
||||
if (maintainAspectRatio && canvasUseNewSize) {
|
||||
newBitmap = CreateEmpty(newWidth, newHeight, sourceBitmap.PixelFormat, backgroundColor, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution);
|
||||
newImage = CreateEmpty(newWidth, newHeight, sourceImage.PixelFormat, backgroundColor, sourceImage.HorizontalResolution, sourceImage.VerticalResolution);
|
||||
} else {
|
||||
newBitmap = CreateEmpty(destWidth, destHeight, sourceBitmap.PixelFormat, backgroundColor, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution);
|
||||
newImage = CreateEmpty(destWidth, destHeight, sourceImage.PixelFormat, backgroundColor, sourceImage.HorizontalResolution, sourceImage.VerticalResolution);
|
||||
}
|
||||
|
||||
using (Graphics graphics = Graphics.FromImage(newBitmap)) {
|
||||
using (Graphics graphics = Graphics.FromImage(newImage)) {
|
||||
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
||||
graphics.DrawImage(sourceBitmap, new Rectangle(destX, destY, destWidth, destHeight), new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height), GraphicsUnit.Pixel);
|
||||
graphics.DrawImage(sourceImage, new Rectangle(destX, destY, destWidth, destHeight), new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), GraphicsUnit.Pixel);
|
||||
}
|
||||
return newBitmap;
|
||||
return newImage;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -247,7 +247,7 @@ namespace GreenshotPlugin.Core {
|
|||
if (string.IsNullOrEmpty(fullPath)) {
|
||||
return null;
|
||||
}
|
||||
Bitmap fileBitmap = null;
|
||||
Image fileImage = null;
|
||||
LOG.InfoFormat("Loading image from file {0}", fullPath);
|
||||
// Fixed lock problem Bug #3431881
|
||||
using (Stream surfaceFileStream = File.OpenRead(fullPath)) {
|
||||
|
@ -258,7 +258,7 @@ namespace GreenshotPlugin.Core {
|
|||
surfaceFileStream.Position = 0;
|
||||
using (Image tmpImage = Image.FromStream(surfaceFileStream, true, true)) {
|
||||
LOG.DebugFormat("Loaded {0} with Size {1}x{2} and PixelFormat {3}", fullPath, tmpImage.Width, tmpImage.Height, tmpImage.PixelFormat);
|
||||
fileBitmap = ImageHelper.Clone(tmpImage);
|
||||
fileImage = ImageHelper.Clone(tmpImage);
|
||||
}
|
||||
// Start at -14 read "GreenshotXX.YY" (XX=Major, YY=Minor)
|
||||
const int markerSize = 14;
|
||||
|
@ -280,9 +280,9 @@ namespace GreenshotPlugin.Core {
|
|||
}
|
||||
}
|
||||
}
|
||||
if (fileBitmap != null) {
|
||||
returnSurface.Image = fileBitmap;
|
||||
LOG.InfoFormat("Information about file {0}: {1}x{2}-{3} Resolution {4}x{5}", fullPath, fileBitmap.Width, fileBitmap.Height, fileBitmap.PixelFormat, fileBitmap.HorizontalResolution, fileBitmap.VerticalResolution);
|
||||
if (fileImage != null) {
|
||||
returnSurface.Image = fileImage;
|
||||
LOG.InfoFormat("Information about file {0}: {1}x{2}-{3} Resolution {4}x{5}", fullPath, fileImage.Width, fileImage.Height, fileImage.PixelFormat, fileImage.HorizontalResolution, fileImage.VerticalResolution);
|
||||
}
|
||||
return returnSurface;
|
||||
}
|
||||
|
|
|
@ -86,16 +86,15 @@ namespace GreenshotPlugin.Core {
|
|||
/// </summary>
|
||||
/// <param name="baseUri"></param>
|
||||
/// <returns>Bitmap</returns>
|
||||
public static Bitmap DownloadImage(string url) {
|
||||
public static Image DownloadImage(string url) {
|
||||
try {
|
||||
HttpWebRequest request = (HttpWebRequest)NetworkHelper.CreateWebRequest(url);
|
||||
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
||||
if (request.HaveResponse) {
|
||||
using (Image image = Image.FromStream(response.GetResponseStream())) {
|
||||
return new Bitmap(image);
|
||||
return ImageHelper.Clone(image);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
LOG.Error("Problem downloading the image from: " + url, e);
|
||||
}
|
||||
|
|
|
@ -103,8 +103,8 @@ namespace Greenshot.Plugin.Drawing {
|
|||
void FitToText();
|
||||
}
|
||||
|
||||
public interface IBitmapContainer: IDrawableContainer {
|
||||
Bitmap Bitmap {
|
||||
public interface IImageContainer: IDrawableContainer {
|
||||
Image Image {
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
|
|
@ -110,14 +110,12 @@ namespace Greenshot.Plugin {
|
|||
/// <param name="fillColor">Color of background (e.g. Color.Transparent)</param>
|
||||
ITextContainer AddTextContainer(string text, HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment, FontFamily family, float size, bool italic, bool bold, bool shadow, int borderSize, Color color, Color fillColor);
|
||||
|
||||
IBitmapContainer AddBitmapContainer(Bitmap bitmap, int x, int y);
|
||||
IImageContainer AddImageContainer(Image image, int x, int y);
|
||||
ICursorContainer AddCursorContainer(Cursor cursor, int x, int y);
|
||||
IIconContainer AddIconContainer(Icon icon, int x, int y);
|
||||
IMetafileContainer AddMetafileContainer(Metafile metafile, int x, int y);
|
||||
IBitmapContainer AddBitmapContainer(string filename, int x, int y);
|
||||
IImageContainer AddImageContainer(string filename, int x, int y);
|
||||
ICursorContainer AddCursorContainer(string filename, int x, int y);
|
||||
IIconContainer AddIconContainer(string filename, int x, int y);
|
||||
IMetafileContainer AddMetafileContainer(string filename, int x, int y);
|
||||
long SaveElementsToStream(Stream stream);
|
||||
void LoadElementsFromStream(Stream stream);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue