-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitmapToText.cs
More file actions
64 lines (55 loc) · 2.28 KB
/
Copy pathBitmapToText.cs
File metadata and controls
64 lines (55 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using System.Drawing;
using System.Security.Cryptography;
namespace HideTextInImage
{
public class BitmapToText
{
public static string Extract(Bitmap image, string originalImageHash = null, int stopAtCharNumber = -1)
{
Color curPixlColor = image.GetPixel(0, 0);
int counter = 0;
char charFromColor;
string text = "";
//checks if stopAtChar argument was given if not the loop will end at the last pixel of the image
if (stopAtCharNumber < 0)
{
curPixlColor = image.GetPixel(image.Width - 1, image.Height - 1);
stopAtCharNumber = PixelColorToNumber(curPixlColor.A, curPixlColor.R, curPixlColor.G, curPixlColor.B);
}
for (int y = 0; y < image.Height; y++)
{
for (int x = 0, len = image.Width; x < image.Width; x++)
{
if (counter < stopAtCharNumber)
{
curPixlColor = image.GetPixel(x, y);
charFromColor = (char)PixelColorToNumber(curPixlColor.A, curPixlColor.R, curPixlColor.G, curPixlColor.B);
text += charFromColor;
counter++;
}
else
{
return text;
}
}
}
if (originalImageHash != null)
{
text = Sha256.Encrypt(text, originalImageHash);
}
return text;
}
//Only the blue and green values are used
static int PixelColorToNumber(int alpha, int red, int green, int blue)
{
string colorAsBinary = String.Format("{0}{1}",
/* Convert.ToString(alpha, 2).PadLeft(8, '0'),
Convert.ToString(red, 2).PadLeft(8, '0'),*/
Convert.ToString(green, 2).PadLeft(8, '0'),
Convert.ToString(blue, 2).PadLeft(8, '0'));
int decimalValue = Convert.ToInt32(colorAsBinary, 2);
return decimalValue;
}
}
}