Add BitmapUtils.cs and more

This commit is contained in:
Namhyeon Go 2024-05-13 16:53:33 +09:00
parent 2408e89940
commit 24dcfdb2cc
3 changed files with 95 additions and 0 deletions

View File

@ -0,0 +1,67 @@
/*
* WelsonJS.Toolkit: WelsonJS native component
*
* filename:
* BitmapUtils.cs
*
* description:
* WelsonJS - Build a Windows app on the Windows built-in JavaScript engine
*
* website:
* - https://github.com/gnh1201/welsonjs
* - https://catswords.social/@catswords_oss
*
* author:
* Namhyeon Go <abuse@catswords.net>
*
* license:
* GPLv3 or MS-RL(Microsoft Reciprocal License)
*
*/
using System.Drawing;
namespace WelsonJS
{
public class BitmapUtils
{
private static Bitmap Load(string filename)
{
return new Bitmap(filename);
}
public static void Crop(string srcfile, string dstfile, int x, int y, int a, int b)
{
Bitmap originalBitmap = Load(srcfile);
Rectangle cropArea = new Rectangle(x, y, a, b);
Bitmap croppedBitmap = originalBitmap.Clone(cropArea, originalBitmap.PixelFormat);
croppedBitmap.Save(dstfile);
}
public static int[] GetSize(string srcfile)
{
Bitmap bitmap = Load(srcfile);
int width = bitmap.Width;
int height = bitmap.Height;
return new int[] { width, height };
}
public static int[] GetPixel(string srcfile, int x, int y)
{
Bitmap bitmap = Load(srcfile);
Color pixelColor = bitmap.GetPixel(x, y);
int red = pixelColor.R;
int green = pixelColor.G;
int blue = pixelColor.B;
bitmap.Dispose();
return new int[] { red, green, blue };
}
}
}

View File

@ -241,5 +241,32 @@ namespace WelsonJS
HIGHT.ECB cipher = new HIGHT.ECB(userKey);
return Encoding.UTF8.GetString(cipher.Decrypt(dataIn)).Trim('\0');
}
[ComVisible(true)]
public string GetImageSize(string srcfile)
{
int[] result = BitmapUtils.GetSize(srcfile);
int width = result[0];
int height = result[1];
return $"width={width}; height={height}";
}
[ComVisible(true)]
public string GetImagePixel(string srcfile)
{
int[] result = BitmapUtils.GetSize(srcfile);
int red = result[0];
int green = result[1];
int blue = result[2];
return $"red={red}; green={green}; blue={blue}";
}
[ComVisible(true)]
public void CropImage(string srcfile, string dstfile, int x, int y, int a, int b)
{
BitmapUtils.Crop(srcfile, dstfile, x, y, a, b);
}
}
}

View File

@ -65,6 +65,7 @@
<Reference Include="System.Windows.Forms" />
</ItemGroup>
<ItemGroup>
<Compile Include="BitmapUtils.cs" />
<Compile Include="Compression\LZ77.cs" />
<Compile Include="Cryptography\HIGHT.cs" />
<Compile Include="NamedSharedMemory.cs" />