2013-11-03 23:53:49 +13:00
#region License Information ( GPL v3 )
/ *
ShareX - A program that allows you to take screenshots and share any file type
2022-01-12 05:32:17 +13:00
Copyright ( c ) 2007 - 2022 ShareX Team
2013-11-03 23:53:49 +13:00
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 2
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 , write to the Free Software
Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 , USA .
Optionally you can also view the license at < http : //www.gnu.org/licenses/>.
* /
#endregion License Information ( GPL v3 )
2014-12-11 09:25:20 +13:00
using ShareX.HelpersLib ;
2020-07-06 10:37:56 +12:00
using System ;
2013-11-03 23:53:49 +13:00
using System.ComponentModel ;
using System.Drawing ;
2013-11-09 16:13:49 +13:00
using System.Drawing.Design ;
2020-07-06 10:37:56 +12:00
using System.Drawing.Drawing2D ;
2020-08-30 06:06:29 +12:00
using System.Drawing.Imaging ;
2013-11-09 16:13:49 +13:00
using System.IO ;
2013-11-03 23:53:49 +13:00
2014-12-11 09:25:20 +13:00
namespace ShareX.ImageEffectsLib
2013-11-03 23:53:49 +13:00
{
2020-07-06 10:37:56 +12:00
[Description("Image")]
2013-11-17 22:30:47 +13:00
public class DrawImage : ImageEffect
2013-11-03 23:53:49 +13:00
{
2019-04-02 01:05:52 +13:00
[DefaultValue(""), Editor(typeof(ImageFileNameEditor), typeof(UITypeEditor))]
public string ImageLocation { get ; set ; }
2020-08-29 13:35:28 +12:00
[DefaultValue(ContentAlignment.TopLeft), TypeConverter(typeof(EnumProperNameConverter))]
2013-11-17 22:30:47 +13:00
public ContentAlignment Placement { get ; set ; }
2020-08-29 13:35:28 +12:00
[DefaultValue(typeof(Point), "0, 0")]
2018-11-05 11:02:06 +13:00
public Point Offset { get ; set ; }
2013-11-03 23:53:49 +13:00
2020-07-06 10:37:56 +12:00
[DefaultValue(DrawImageSizeMode.DontResize), Description("How the image watermark should be rescaled, if at all."), TypeConverter(typeof(EnumDescriptionConverter))]
2018-02-24 05:21:24 +13:00
public DrawImageSizeMode SizeMode { get ; set ; }
2018-02-21 07:21:45 +13:00
[DefaultValue(typeof(Size), "0, 0")]
public Size Size { get ; set ; }
2021-02-15 20:31:26 +13:00
[DefaultValue(ImageRotateFlipType.None), TypeConverter(typeof(EnumProperNameKeepCaseConverter))]
public ImageRotateFlipType RotateFlip { get ; set ; }
2020-11-16 04:56:11 +13:00
[DefaultValue(false)]
public bool Tile { get ; set ; }
2020-08-29 11:46:41 +12:00
[DefaultValue(false), Description("If image watermark size bigger than source image then don't draw it.")]
public bool AutoHide { get ; set ; }
2020-07-06 10:37:56 +12:00
[DefaultValue(ImageInterpolationMode.HighQualityBicubic), TypeConverter(typeof(EnumProperNameConverter))]
public ImageInterpolationMode InterpolationMode { get ; set ; }
[DefaultValue(CompositingMode.SourceOver), TypeConverter(typeof(EnumProperNameConverter))]
public CompositingMode CompositingMode { get ; set ; }
2020-08-29 11:46:41 +12:00
private int opacity ;
[DefaultValue(100)]
public int Opacity
{
get
{
return opacity ;
}
set
{
opacity = value . Clamp ( 0 , 100 ) ;
}
}
2019-04-02 01:05:52 +13:00
2013-11-09 16:13:49 +13:00
public DrawImage ( )
2013-11-03 23:53:49 +13:00
{
this . ApplyDefaultPropertyValues ( ) ;
}
2020-03-22 10:16:55 +13:00
public override Bitmap Apply ( Bitmap bmp )
2013-11-03 23:53:49 +13:00
{
2020-11-16 04:44:37 +13:00
if ( Opacity < 1 | | ( SizeMode ! = DrawImageSizeMode . DontResize & & Size . Width < = 0 & & Size . Height < = 0 ) )
2020-07-06 10:37:56 +12:00
{
return bmp ;
}
string imageFilePath = Helpers . E xpandFolderVariables ( ImageLocation , true ) ;
2020-05-14 09:18:27 +12:00
if ( ! string . IsNullOrEmpty ( imageFilePath ) & & File . Exists ( imageFilePath ) )
2013-11-09 13:56:37 +13:00
{
2020-07-06 10:37:56 +12:00
using ( Bitmap bmpWatermark = ImageHelpers . LoadImage ( imageFilePath ) )
2013-11-09 16:13:49 +13:00
{
2020-07-06 10:37:56 +12:00
if ( bmpWatermark ! = null )
2018-02-21 07:21:45 +13:00
{
2021-02-15 20:31:26 +13:00
if ( RotateFlip ! = ImageRotateFlipType . None )
{
bmpWatermark . RotateFlip ( ( RotateFlipType ) RotateFlip ) ;
}
2020-07-06 10:37:56 +12:00
Size imageSize ;
2020-11-16 04:44:37 +13:00
if ( SizeMode = = DrawImageSizeMode . AbsoluteSize )
2019-04-02 01:05:52 +13:00
{
2020-09-12 07:37:25 +12:00
int width = Size . Width = = - 1 ? bmp . Width : Size . Width ;
int height = Size . Height = = - 1 ? bmp . Height : Size . Height ;
imageSize = ImageHelpers . ApplyAspectRatio ( width , height , bmpWatermark ) ;
2019-04-02 01:05:52 +13:00
}
2019-12-08 20:23:20 +13:00
else if ( SizeMode = = DrawImageSizeMode . PercentageOfWatermark )
{
2020-07-06 10:37:56 +12:00
int width = ( int ) Math . Round ( Size . Width / 100f * bmpWatermark . Width ) ;
int height = ( int ) Math . Round ( Size . Height / 100f * bmpWatermark . Height ) ;
imageSize = ImageHelpers . ApplyAspectRatio ( width , height , bmpWatermark ) ;
2019-12-08 20:23:20 +13:00
}
else if ( SizeMode = = DrawImageSizeMode . PercentageOfCanvas )
2019-04-02 01:05:52 +13:00
{
2020-07-06 10:37:56 +12:00
int width = ( int ) Math . Round ( Size . Width / 100f * bmp . Width ) ;
int height = ( int ) Math . Round ( Size . Height / 100f * bmp . Height ) ;
imageSize = ImageHelpers . ApplyAspectRatio ( width , height , bmpWatermark ) ;
}
else
{
imageSize = bmpWatermark . Size ;
2019-04-02 01:05:52 +13:00
}
2020-03-22 10:16:55 +13:00
Point imagePosition = Helpers . GetPosition ( Placement , Offset , bmp . Size , imageSize ) ;
2019-12-08 20:23:20 +13:00
Rectangle imageRectangle = new Rectangle ( imagePosition , imageSize ) ;
2013-11-17 20:26:41 +13:00
2020-03-22 10:16:55 +13:00
if ( AutoHide & & ! new Rectangle ( 0 , 0 , bmp . Width , bmp . Height ) . Contains ( imageRectangle ) )
2019-12-08 20:23:20 +13:00
{
2020-03-22 10:16:55 +13:00
return bmp ;
2019-12-08 20:23:20 +13:00
}
2013-11-09 16:22:40 +13:00
2020-03-22 10:16:55 +13:00
using ( Graphics g = Graphics . FromImage ( bmp ) )
2019-12-08 20:23:20 +13:00
{
2020-07-06 10:37:56 +12:00
g . InterpolationMode = ImageHelpers . GetInterpolationMode ( InterpolationMode ) ;
g . PixelOffsetMode = PixelOffsetMode . Half ;
g . CompositingMode = CompositingMode ;
2020-08-29 11:46:41 +12:00
2020-11-16 04:56:11 +13:00
if ( Tile )
{
using ( TextureBrush brush = new TextureBrush ( bmpWatermark , WrapMode . Tile ) )
{
brush . TranslateTransform ( imageRectangle . X , imageRectangle . Y ) ;
g . FillRectangle ( brush , imageRectangle ) ;
}
}
else if ( Opacity < 100 )
2020-08-29 11:46:41 +12:00
{
2020-11-16 04:44:37 +13:00
using ( ImageAttributes ia = new ImageAttributes ( ) )
2020-08-29 11:46:41 +12:00
{
2020-11-16 04:44:37 +13:00
ColorMatrix matrix = ColorMatrixManager . Alpha ( Opacity / 100f ) ;
ia . SetColorMatrix ( matrix , ColorMatrixFlag . Default , ColorAdjustType . Bitmap ) ;
g . DrawImage ( bmpWatermark , imageRectangle , 0 , 0 , bmpWatermark . Width , bmpWatermark . Height , GraphicsUnit . Pixel , ia ) ;
2020-08-29 11:46:41 +12:00
}
}
else
{
2020-11-16 04:44:37 +13:00
g . DrawImage ( bmpWatermark , imageRectangle ) ;
2020-08-29 11:46:41 +12:00
}
2019-12-08 20:23:20 +13:00
}
2013-11-09 16:13:49 +13:00
}
}
2013-11-09 13:56:37 +13:00
}
2013-11-09 16:13:49 +13:00
2020-03-22 10:16:55 +13:00
return bmp ;
2018-02-24 05:21:24 +13:00
}
2013-11-03 23:53:49 +13:00
}
}