Created
May 30, 2021 07:17
-
-
Save deadmann/1e7a81bd21927435ccf3e7e404190efb to your computer and use it in GitHub Desktop.
Image Helper - Resize, file and set watermark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Drawing; | |
using System.Drawing.Imaging; // .Net Core Fix: Image and ImageFormat are missing and it requires to install `System.Drawing.Coomon` package | |
using System.IO; | |
namespace Hassan.Helpers | |
{ | |
internal class ImageHelper | |
{ | |
internal static void SaveImageToFile(Image img, string staticFileName) | |
{ | |
using(FileStream fs = new FileStream(staticFileName, FileMode.Create, FileAccess.Write)) | |
img.Save(fs, ImageFormat.Jpeg); | |
} | |
internal static Image GetImageFromFile(string staticFileAddress) | |
{ | |
using (FileStream fs = new FileStream(staticFileAddress, FileMode.Open, FileAccess.Read)) | |
{ | |
return Image.FromStream(fs); | |
} | |
} | |
internal static Image ResizeImage(Image inputBmp, int resizeWidth) | |
{ | |
int thumbHeight = (int)((inputBmp.Height / (float)inputBmp.Width) * resizeWidth); | |
Image resultBmp = new Bitmap(resizeWidth, thumbHeight); | |
//resultBmp.SetResolution(72, 72); //only bitmap | |
using (Graphics g = Graphics.FromImage(resultBmp)) | |
{ | |
g.DrawImage(inputBmp, new Rectangle(0, 0, resultBmp.Width, resultBmp.Height), 0, 0, inputBmp.Width, inputBmp.Height, GraphicsUnit.Pixel); | |
g.Flush(); | |
} | |
return resultBmp; | |
} | |
internal static Image SetWatermarkCornerImageInPercentOfTotal(Image inputImg, Image watermarkImg, Corner corner, | |
float sizePercentage, float marginWidthPercentage, float marginHeightPercentage) | |
{ | |
if(sizePercentage<= 0 || sizePercentage>=100) throw new ArgumentOutOfRangeException(nameof(sizePercentage), "Argument should be between 0 and 100"); | |
if (marginWidthPercentage <= 0 || marginWidthPercentage >= 100) throw new ArgumentOutOfRangeException(nameof(marginWidthPercentage), "Argument should be between 0 and 100"); | |
if (marginHeightPercentage <= 0 || marginHeightPercentage >= 100) throw new ArgumentOutOfRangeException(nameof(marginHeightPercentage), "Argument should be between 0 and 100"); | |
int watermarkWidth = (int)(inputImg.Width * sizePercentage / 100f); | |
int marginWidth = (int)(inputImg.Width * marginWidthPercentage / 100f); | |
int marginHeight = (int)(inputImg.Height * marginHeightPercentage / 100f); | |
return WatermarkCornerImage(inputImg, watermarkImg, corner, watermarkWidth, marginWidth, marginHeight); | |
} | |
internal static Image SetWatermarkCenterImageInPercentOfTotal(Image inputImg, Image watermarkImg, int watermarkWidthPercentage) | |
{ | |
if (watermarkWidthPercentage <= 0 || watermarkWidthPercentage >= 100) throw new ArgumentOutOfRangeException(nameof(watermarkWidthPercentage), "Argument should be between 0 and 100"); | |
int watermarkWidth = (int)(inputImg.Width * watermarkWidthPercentage / 100f); | |
return WatermarkCenterImage(inputImg, watermarkImg, watermarkWidth); | |
} | |
internal static Image WatermarkCornerImage(Image inputImg, Image watermarkImg,Corner corner, int watermarkWidth, int marginWidth, int marginHeight) | |
{ | |
int watermarkHeight = (int)((watermarkImg.Height / (float)watermarkImg.Width) * watermarkWidth); | |
int watermarkLeft; | |
int watermarkTop; | |
switch (corner) | |
{ | |
case Corner.TopLeft: | |
watermarkLeft = marginWidth; | |
watermarkTop = marginHeight; | |
break; | |
case Corner.TopRight: | |
watermarkLeft = inputImg.Width - (watermarkWidth + marginWidth); | |
watermarkTop = marginHeight; | |
break; | |
case Corner.BottomRight: | |
watermarkLeft = inputImg.Width - (watermarkWidth + marginWidth); | |
watermarkTop = inputImg.Height - (watermarkHeight + marginHeight); | |
break; | |
case Corner.BottomLeft: | |
watermarkLeft = marginWidth; | |
watermarkTop = inputImg.Height - (watermarkHeight + marginHeight); | |
break; | |
default: | |
throw new ArgumentOutOfRangeException(nameof(corner), | |
"Provided value for for the enum of Corner type, is not valid"); | |
} | |
Bitmap resultBmp = new Bitmap(inputImg.Width, inputImg.Height); | |
//resultBmp.SetResolution(72, 72); //only bitmap/not image | |
resultBmp.SetResolution(inputImg.HorizontalResolution, inputImg.VerticalResolution); | |
using (Graphics g = Graphics.FromImage(resultBmp)) | |
{ | |
g.DrawImage(inputImg, 0, 0); | |
g.DrawImage(watermarkImg, watermarkLeft, watermarkTop, watermarkWidth, watermarkHeight); | |
g.Flush(); | |
} | |
return resultBmp; | |
} | |
internal static Image WatermarkCenterImage(Image inputImg, Image watermark, int watermarkWidth) | |
{ | |
int watermarkHeight = (int)((watermark.Height / (float)watermark.Width) * watermarkWidth); | |
int watermarkLeft = (inputImg.Width / 2) - (watermarkWidth / 2); | |
int watermarkTop = (inputImg.Height / 2) - (watermarkHeight / 2); | |
Bitmap resultBmp = new Bitmap(inputImg.Width, inputImg.Height); | |
//resultBmp.SetResolution(72, 72); //only bitmap/not image | |
resultBmp.SetResolution(inputImg.HorizontalResolution, inputImg.VerticalResolution); //cause crop | |
using (Graphics g = Graphics.FromImage(resultBmp)) | |
{ | |
g.DrawImage(inputImg, 0, 0); | |
g.DrawImage(watermark, watermarkLeft, watermarkTop, watermarkWidth, watermarkHeight); | |
g.Flush(); | |
} | |
return resultBmp; | |
} | |
} | |
internal enum Corner | |
{ | |
TopLeft, | |
TopRight, | |
BottomRight, | |
BottomLeft | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
wrote 3~5 years ago