code explanation
-
2012年2月26日 5:14
i need the explanation of this code which is written below
loadedTrueImagePath = openFileDialog1.FileName;
EnImage_tbx.Text = loadedTrueImagePath;
loadedTrueImage = Image.FromFile(loadedTrueImagePath);
height = loadedTrueImage.Height;
width = loadedTrueImage.Width;
loadedTrueBitmap = newBitmap(loadedTrueImage);
FileInfoimginf = newFileInfo(loadedTrueImagePath);
floatfs = (float)imginf.Length / 1024;
ImageSize_lbl.Text = smalldecimal(fs.ToString(), 2) + " KB";
ImageHeight_lbl.Text = loadedTrueImage.Height.ToString() + " Pixel";
すべての返信
-
2012年2月26日 6:11
1. loadedTrueImagePath = openFileDialog1.FileName; ----------------------> Get the file name selected from OpenFileDialog
2. EnImage_tbx.Text = loadedTrueImagePath; ------------------------------> Strore the file name in control named EnImage_tbx
3. oadedTrueImage = Image.FromFile(loadedTrueImagePath);------------> Create an Image object from the file selected
4. height = loadedTrueImage.Height;------------------------------------------> Get the height of the image and store it in height variable
5. width = loadedTrueImage.Width;---------------------------------------------> Get the Width of the image and store it in width variable
6. loadedTrueBitmap = newBitmap(loadedTrueImage);-----------------------> Create a Bitmap object from the image create in Step 3
7. FileInfoimginf = newFileInfo(loadedTrueImagePath);--------------------- > Create FileInfo object from the image in step 3.
8. floatfs = (float)imginf.Length / 1024;----------------------------------------> Get the size of the file in Kilo Bytes
9. ImageSize_lbl.Text = smalldecimal(fs.ToString(), 2) + " KB";------------- > Display the size of the file in KiloBytes on Label named ImageSize_lbl.
10. ImageHeight_lbl.Text = loadedTrueImage.Height.ToString() + " Pixel";-> Display the height of the file in pixels on Label named ImageHeight_lbl.
I hope this helps.
Please mark this post as answer if it solved your problem. Happy Programming!
- 回答の候補に設定 Mitja BoncaMicrosoft Community Contributor 2012年2月26日 7:56
- 回答としてマーク Yoyo JiangMicrosoft Contingent Staff, Moderator 2012年3月8日 2:56
-
2012年2月26日 6:21
I need the explanation of the code which is written below
FileInfo imginf = new FileInfo(loadedTrueImagePath);
floatfs = (float)imginf.Length / 1024;
ImageSize_lbl.Text = smalldecimal(fs.ToString(), 2) + " KB";
ImageHeight_lbl.Text = loadedTrueImage.Height.ToString() + " Pixel";
ImageWidth_lbl.Text = loadedTrueImage.Width.ToString() + " Pixel";
doublecansave = (8.0 * ((height * (width / 3) * 3) / 3 - 1)) / 1024;
CanSave_lbl.Text = smalldecimal(cansave.ToString(), 2) + " KB";
canPaint = true;
this.Invalidate();
}
}
privatestringsmalldecimal(stringinp, intdec)
{
inti;
for(i = inp.Length - 1; i > 0; i--)
if(inp[i] == '.')
break;
try
{
returninp.Substring(0, i + dec + 1);
}
catch
{
returninp;
}
}
privatevoidEnFileBrowse_btn_Click(objectsender, EventArgse)
{
if(openFileDialog2.ShowDialog() == DialogResult.OK)
{
loadedFilePath = openFileDialog2.FileName;
EnFile_tbx.Text = loadedFilePath;
FileInfofinfo = newFileInfo(loadedFilePath);
fileSize = finfo.Length;
fileNameSize = justFName(loadedFilePath).Length;
}
}
- 編集済み swati88 2012年2月26日 6:38
- 結合 Yoyo JiangMicrosoft Contingent Staff, Moderator 2012年2月27日 9:06 Merge for better discussion..
-
2012年2月26日 6:34
I need the explanation of this code for encryption which is written below:
privatevoidEncryptLayer()
{
toolStripStatusLabel1.Text ="Encrypting... Please wait";
Application.DoEvents();
longFSize = fileSize;
BitmapchangedBitmap = EncryptLayer(8, loadedTrueBitmap, 0, (height * (width/3)*3) / 3 - fileNameSize - 1, true);
FSize -= (height * (width / 3) * 3) / 3 - fileNameSize - 1;
if(FSize > 0)
{
for(inti = 7; i >= 0 && FSize > 0; i--)
{
changedBitmap = EncryptLayer(i, changedBitmap, (((8 - i) * height * (width / 3) * 3) / 3 - fileNameSize - (8 - i)), (((9 - i) * height * (width / 3) * 3) / 3 - fileNameSize - (9 - i)), false);
FSize -= (height * (width / 3) * 3) / 3 - 1;
}
}
changedBitmap.Save(saveToImage);
toolStripStatusLabel1.Text = "Encrypted image has been successfully saved.";
EncriptionDone = true;
AfterEncryption = Image.FromFile(saveToImage);
this.Invalidate();
}
privateBitmapEncryptLayer(intlayer, BitmapinputBitmap, longstartPosition, longendPosition, boolwriteFileName)
{
BitmapoutputBitmap = inputBitmap;
layer--;
inti = 0, j = 0;
longFNSize = 0;
bool[] t = newbool[8];
bool[] rb = newbool[8];
bool[] gb = newbool[8];
bool[] bb = newbool[8];
Colorpixel = newColor();
byter, g, b;
if(writeFileName)
{
FNSize = fileNameSize;
stringfileName = justFName(loadedFilePath);
//write fileName:
for(i = 0; i < height && i * (height / 3) < fileNameSize; i++)
for(j = 0; j < (width / 3) * 3 && i * (height / 3) + (j / 3) < fileNameSize; j++)
{
byte2bool((byte)fileName[i * (height / 3) + j / 3], reft);
pixel = inputBitmap.GetPixel(j, i);
r = pixel.R;
g = pixel.G;
b = pixel.B;
byte2bool(r, refrb);
byte2bool(g, refgb);
byte2bool(b, refbb);
if(j % 3 == 0)
{
rb[7] = t[0];
gb[7] = t[1];
bb[7] = t[2];
}
elseif(j % 3 == 1)
{
rb[7] = t[3];
gb[7] = t[4];
bb[7] = t[5];
}
else
{
rb[7] = t[6];
gb[7] = t[7];
}
Colorresult = Color.FromArgb((int)bool2byte(rb), (int)bool2byte(gb), (int)bool2byte(bb));
outputBitmap.SetPixel(j, i, result);
}
i--;
}
//write file (after file name):
inttempj = j;
for(; i < height && i * (height / 3) < endPosition - startPosition + FNSize && startPosition + i * (height / 3) < fileSize + FNSize; i++)
for(j = 0; j < (width / 3) * 3 && i * (height / 3) + (j / 3) < endPosition - startPosition + FNSize && startPosition + i * (height / 3) + (j / 3) < fileSize + FNSize; j++)
{
if(tempj != 0)
{
j = tempj;
tempj = 0;
}
byte2bool((byte)fileContainer[startPosition + i * (height / 3) + j / 3 - FNSize], reft);
pixel = inputBitmap.GetPixel(j, i);
r = pixel.R;
g = pixel.G;
b = pixel.B;
byte2bool(r, refrb);
byte2bool(g, refgb);
byte2bool(b, refbb);
if(j % 3 == 0)
{
rb[layer] = t[0];
gb[layer] = t[1];
bb[layer] = t[2];
}
elseif(j % 3 == 1)
{
rb[layer] = t[3];
gb[layer] = t[4];
bb[layer] = t[5];
}
else
{
rb[layer] = t[6];
gb[layer] = t[7];
}
Colorresult = Color.FromArgb((int)bool2byte(rb), (int)bool2byte(gb), (int)bool2byte(bb));
outputBitmap.SetPixel(j, i, result);
}
longtempFS = fileSize, tempFNS = fileNameSize;
r = (byte)(tempFS % 100);
tempFS /= 100;
g = (byte)(tempFS % 100);
tempFS /= 100;
b = (byte)(tempFS % 100);
ColorflenColor = Color.FromArgb(r,g,b);
outputBitmap.SetPixel(width - 1, height - 1, flenColor);
r = (byte)(tempFNS % 100);
tempFNS /= 100;
g = (byte)(tempFNS % 100);
tempFNS /= 100;
b = (byte)(tempFNS % 100);
ColorfnlenColor = Color.FromArgb(r,g,b);
outputBitmap.SetPixel(width - 2, height - 1, fnlenColor);
returnoutputBitmap;
}- 編集済み swati88 2012年2月26日 6:39
- 結合 Yoyo JiangMicrosoft Contingent Staff, Moderator 2012年2月27日 9:07 Merge for better discussion..
-
2012年2月26日 6:47plz explanation this code....... i need it........
-
2012年2月26日 7:49
Hello Swati,
private string smalldecimal(string inp, int dec) { int i; for(i = inp.Length - 1; i > 0; i--) if(inp[i] == '.') break; try { return inp.Substring(0, i + dec + 1); } catch { return inp; } }this function make "Roundingdecimals" as following
smalldecimal("10.111",2) = "10.11"
smalldecimal("11.235",1) = "11.2"
smalldecimal("12",3) = "12"
smalldecimal("10.111",0) = "10."
private void EnFileBrowse_btn_Click(objectsender, EventArgse) { if(openFileDialog2.ShowDialog() == DialogResult.OK) { loadedFilePath = openFileDialog2.FileName; EnFile_tbx.Text = loadedFilePath; FileInfofinfo = newFileInfo(loadedFilePath); fileSize = finfo.Length; fileNameSize = justFName(loadedFilePath).Length; } }this is the handler of Browse button which show "OpenFileDialog" to get file & show it's data in TextBox "EnFile_tbx" & get some else data.
FileInfo imginf = new FileInfo(loadedTrueImagePath); floatfs = (float)imginf.Length / 1024; ImageSize_lbl.Text = smalldecimal(fs.ToString(), 2) + " KB"; ImageHeight_lbl.Text = loadedTrueImage.Height.ToString() + " Pixel"; ImageWidth_lbl.Text = loadedTrueImage.Width.ToString() + " Pixel"; doublecansave = (8.0 * ((height * (width / 3) * 3) / 3 - 1)) / 1024; CanSave_lbl.Text = smalldecimal(cansave.ToString(), 2) + " KB"; canPaint = true; this.Invalidate(); } }this function too make some operations to get some data about file.
- 回答としてマーク Yoyo JiangMicrosoft Contingent Staff, Moderator 2012年3月8日 2:56
-
2012年2月26日 7:57Can`t be a better explanation. Well done Adavesh :)
Mitja
-
2012年2月26日 11:28
Can`t be a better explanation. Well done Adavesh :)
Mitja
:) Thanks Mitja.Please mark this post as answer if it solved your problem. Happy Programming!
-
2012年2月26日 14:53thanx it's a little bit helpful, i need some more explanations of last where functions make operations........
- 編集済み swati88 2012年2月26日 15:06
-
2012年2月28日 8:20モデレータ
Hi swati88,
Welcome to the MSDN forum!
I'm trying to explain the code as follow:
private void EncryptLayer() { toolStripStatusLabel1.Text ="Encrypting... Please wait"; // Processes all Windows messages currently in the message queue. //http://msdn.microsoft.com/en-us/library/system.windows.forms.application.doevents.aspx Application.DoEvents(); long FSize = fileSize; // Get the file size. // Encrpt the loaded bitmap. // Call the // private Bitmap EncryptLayer(int layer, Bitmap inputBitmap, long startPosition, long endPosition, bool writeFileName) // method. Bitmap changedBitmap = EncryptLayer(8, loadedTrueBitmap, 0, (height * (width/3)*3) / 3 - fileNameSize - 1, true); FSize -= (height * (width / 3) * 3) / 3 - fileNameSize - 1; if(FSize > 0) { for(int i = 7; i >= 0 && FSize > 0; i--) { changedBitmap = EncryptLayer(i, changedBitmap, (((8 - i) * height * (width / 3) * 3) / 3 - fileNameSize - (8 - i)), (((9 - i) * height * (width / 3) * 3) / 3 - fileNameSize - (9 - i)), false); FSize -= (height * (width / 3) * 3) / 3 - 1; } } changedBitmap.Save(saveToImage); toolStripStatusLabel1.Text = "Encrypted image has been successfully saved."; EncriptionDone = true; AfterEncryption = Image.FromFile(saveToImage); // Refresh the UI. this.Invalidate(); } private Bitmap EncryptLayer(int layer, Bitmap inputBitmap, long startPosition, long endPosition, bool writeFileName) { // The Bitmap to be encrypted. Bitmap outputBitmap = inputBitmap; layer--; int i = 0, j = 0; long FNSize = 0; bool[] t = new bool[8]; bool[] rb = new bool[8]; bool[] gb = new bool[8]; bool[] bb = new bool[8]; Color pixel = new Color(); byte r, g, b; if(writeFileName) { FNSize = fileNameSize; string fileName = justFName(loadedFilePath); //write fileName: for(i = 0; i < height && i * (height / 3) < fileNameSize; i++) for(j = 0; j < (width / 3) * 3 && i * (height / 3) + (j / 3) < fileNameSize; j++) { // Based on my understanding, it seems to get the R, G, B information // from the origin bitmap. #region byte2bool((byte)fileName[i * (height / 3) + j / 3], ref t); pixel = inputBitmap.GetPixel(j, i); r = pixel.R; g = pixel.G; b = pixel.B; byte2bool(r, ref rb); byte2bool(g, ref gb); byte2bool(b, ref bb); #endregion // Encrypt the RGB info. if (j % 3 == 0) { rb[7] = t[0]; gb[7] = t[1]; bb[7] = t[2]; } else if(j % 3 == 1) { rb[7] = t[3]; gb[7] = t[4]; bb[7] = t[5]; } else { rb[7] = t[6]; gb[7] = t[7]; } Color result = Color.FromArgb((int)bool2byte(rb), (int)bool2byte(gb), (int)bool2byte(bb)); outputBitmap.SetPixel(j, i, result); } i--; } //write file (after file name): int tempj = j; for(; i < height && i * (height / 3) < endPosition - startPosition + FNSize && startPosition + i * (height / 3) < fileSize + FNSize; i++) for(j = 0; j < (width / 3) * 3 && i * (height / 3) + (j / 3) < endPosition - startPosition + FNSize && startPosition + i * (height / 3) + (j / 3) < fileSize + FNSize; j++) { if(tempj != 0) { j = tempj; tempj = 0; } byte2bool((byte)fileContainer[startPosition + i * (height / 3) + j / 3 - FNSize], reft); pixel = inputBitmap.GetPixel(j, i); r = pixel.R; g = pixel.G; b = pixel.B; byte2bool(r, refrb); byte2bool(g, refgb); byte2bool(b, refbb); if(j % 3 == 0) { rb[layer] = t[0]; gb[layer] = t[1]; bb[layer] = t[2]; } else if(j % 3 == 1) { rb[layer] = t[3]; gb[layer] = t[4]; bb[layer] = t[5]; } else { rb[layer] = t[6]; gb[layer] = t[7]; } Colorresult = Color.FromArgb((int)bool2byte(rb), (int)bool2byte(gb), (int)bool2byte(bb)); outputBitmap.SetPixel(j, i, result); } long tempFS = fileSize, tempFNS = fileNameSize; r = (byte)(tempFS % 100); tempFS /= 100; g = (byte)(tempFS % 100); tempFS /= 100; b = (byte)(tempFS % 100); Color flenColor = Color.FromArgb(r,g,b); outputBitmap.SetPixel(width - 1, height - 1, flenColor); r = (byte)(tempFNS % 100); tempFNS /= 100; g = (byte)(tempFNS % 100); tempFNS /= 100; b = (byte)(tempFNS % 100); Color fnlenColor = Color.FromArgb(r,g,b); outputBitmap.SetPixel(width - 2, height - 1, fnlenColor); return outputBitmap; }
Also, if you want to look into how these encryption are processed, I suggest you look into the structure of the Bitmap:
Bitmap Structures
http://msdn.microsoft.com/en-us/library/dd183392(v=vs.85).aspxMeanwhile, when I not sure about the code, I usually debug the code step by step, and using "Add Watch" window to watch the variables, and in most cases, it works for me.
Hope it helps and have a nice day!
yoyo
Yoyo Jiang[MSFT]
MSDN Community Support | Feedback to us
- 編集済み Yoyo JiangMicrosoft Contingent Staff, Moderator 2012年3月4日 7:15
- 回答としてマーク swati88 2012年3月8日 6:40
-
2012年3月3日 5:58I can't understand the operation and plz tell me why we use layers for encru\yption in this?
swati mishra
-
2012年3月3日 5:59i can't understand the operation and tell me why we use 8 layers in encryption..........
swati mishra
-
2012年3月5日 5:18モデレータ
Hi swati,
IMHO, the operation encrypts the filename and the file into the bitmap. It encrypts the information into the bitmap line by line from width aspect.
For the layers part, based on my understanding, the layers here is just for increasing the complexity of the encryption.
What is more, since there is “layer--” in the encryption part, I think the following part:
for(int i = 7; i >= 0 && FSize > 0; i--) { changedBitmap = EncryptLayer(i, changedBitmap, (((8 - i) * height * (width / 3) * 3) / 3 - fileNameSize - (8 - i)), (((9 - i) * height * (width / 3) * 3) / 3 - fileNameSize - (9 - i)), false); FSize -= (height * (width / 3) * 3) / 3 - 1; }should be
for(int i = 7; i >1 && FSize > 0; i--) { changedBitmap = EncryptLayer(i, changedBitmap, (((8 - i) * height * (width / 3) * 3) / 3 - fileNameSize - (8 - i)), (((9 - i) * height * (width / 3) * 3) / 3 - fileNameSize - (9 - i)), false); FSize -= (height * (width / 3) * 3) / 3 - 1; }Otherwise, there may be an index overflow exception when the i=0.
Hope it helps.
Have a nice day!yoyo
Yoyo Jiang[MSFT]
MSDN Community Support | Feedback to us
- 回答としてマーク Yoyo JiangMicrosoft Contingent Staff, Moderator 2012年3月8日 2:56
-
2012年3月5日 14:27
how we find the value of r and why we use tempFSr = (byte)(tempFS % 100); tempFS /= 100;
swati mishra
-
2012年3月6日 2:31モデレータ
long tempFS = fileSize, tempFNS = fileNameSize; r = (byte)(tempFS % 100); tempFS /= 100; g = (byte)(tempFS % 100); tempFS /= 100; b = (byte)(tempFS % 100); Color flenColor = Color.FromArgb(r,g,b); outputBitmap.SetPixel(width - 1, height - 1, flenColor);I think here, it just encrypts the file size information into the bitmap, since tempFS = fileNameSize at the beginning.
r = (byte)(tempFNS%100);
Here, % operator is remainder operator. It gets the remainder of (tempFNS/100), and convert the result to byte, then assign it to r.
Then tempFNS/=100; meaning tempFNS = tempFNS/100; then it get the value of g in the same way. Then it is the value of b. In the end, it set the color using the rgb of the (width -1, height -1) pixel.
Hope it helps.
Yoyo Jiang[MSFT]
MSDN Community Support | Feedback to us
- 回答としてマーク swati88 2012年3月8日 6:39
-
2012年3月6日 3:59in encryption we use the key to encrypt message , what is that key and how we encrypt the message in this and as u say layers are used to increase the complexity but how it encrypt?
swati mishra
-
2012年3月7日 3:27モデレータ
Hi swati,
I'd like to take the layer here as the key, since I think the key is used for encryption and decryption. You asked "how we encrypt the message in this". For this, I have to mention that the way is showed in the EncryptLayer method. It used the writeFileName parameter to specify whether the FileName should be encrypt. After encrypting the FileName, it encrypt the data in the file, namely the message you mentioned in the last post.
I suggest you try to contact the author of the code for further discussion about this encryption approach.
Have a nice day!
yoyo
Yoyo Jiang[MSFT]
MSDN Community Support | Feedback to us
- 回答としてマーク swati88 2012年3月8日 6:39
-
2012年3月8日 2:59モデレータ
Hi swati,
I temporarily mark some replies as answers and you can unmark them if they provides no help.
I will keep an eye on the thread in the future. If you have any questions, please feel free to let us know.
Thanks for your understanding and have a nice day!
yoyo
Yoyo Jiang[MSFT]
MSDN Community Support | Feedback to us
-
2012年3月8日 6:40thanx..........
swati mishra
-
2012年4月18日 12:45
Hi.. I have a small doubt here. Can i know to choose only the text or document file to encrypt it. Because in this coding, we can choose any type of file even the image file to be encrypted. Can you help me to solve it.
Thank you.

