Click here to Skip to main content
15,895,192 members
Home / Discussions / C#
   

C#

 
AnswerRe: GetPixel from Graphics object Pin
Robert Rohde9-Apr-06 22:56
Robert Rohde9-Apr-06 22:56 
QuestionHelp Code Pin
nwr_mn9-Apr-06 6:16
nwr_mn9-Apr-06 6:16 
AnswerRe: Help Code Pin
User 66589-Apr-06 7:21
User 66589-Apr-06 7:21 
QuestionHelp me on Steganography Pin
nwr_mn9-Apr-06 6:13
nwr_mn9-Apr-06 6:13 
AnswerRe: Help me on Steganography Pin
Joshua Quick9-Apr-06 11:37
Joshua Quick9-Apr-06 11:37 
QuestionHelp me on Steganography Pin
nwr_mn9-Apr-06 6:03
nwr_mn9-Apr-06 6:03 
AnswerRe: Help me on Steganography Pin
Paul Conrad9-Apr-06 6:57
professionalPaul Conrad9-Apr-06 6:57 
QuestionHelp on the following Code in Steganography in Bitmaps Pin
nwr_mn9-Apr-06 5:58
nwr_mn9-Apr-06 5:58 
public class CryptUtility {

/// Hides a message in a bitmap
/// <param name="messageStream" />The message to hide
/// <param name="bitmap" />The carrier bitmap
/// <param name="keyStream" />The key to use
public static void HideMessageInBitmap(Stream messageStream, Bitmap bitmap, Stream keyStream, bool useGrayscale){
HideOrExtract(ref messageStream, bitmap, keyStream, false, useGrayscale);
messageStream = null;
}

/// Extracts an hidden message from a bitmap
/// <param name="bitmap" />The carrier bitmap
/// <param name="keyStream" />The key used for hiding the message
/// <param name="messageStream" />Empty stream to receive the message
public static void ExtractMessageFromBitmap(Bitmap bitmap, Stream keyStream, ref Stream messageStream){
HideOrExtract(ref messageStream, bitmap, keyStream, true, false);
}

/// Stepts through the pixels of a bitmap using a key pattern and hides or extracts a message
/// <param name="messageStream" />If exctract is false, the message to hide - otherwise an empty stream to receive the extracted message
/// <param name="bitmap" />The carrier bitmap
/// <param name="keyStream" />The key specifying the unchanged pixels between two hidden bytes
/// <param name="extract" />Extract a hidden message (true), or hide a message in a clean carrier bitmap (false)
private static void HideOrExtract(ref Stream messageStream, Bitmap bitmap, Stream keyStream, bool extract, bool useGrayscale){
//Current count of pixels between two hidden message-bytes
//Changes with every hidden byte according to the key
int currentStepWidth = 0;
//Current byte in the key stream - normal direction
byte currentKeyByte;
//Current byte in the key stream - reverse direction
byte currentReverseKeyByte;
//current position in the key stream
long keyPosition;

//maximum X and Y position
int bitmapWidth = bitmap.Width-1;
int bitmapHeight = bitmap.Height-1;

//Color component to hide the next byte in (0-R, 1-G, 2-B)
//Rotates with every hidden byte
int currentColorComponent = 0;

//Stores the color of a pixel
Color pixelColor;

//Length of the message
Int32 messageLength;

if(extract){
//Read the length of the hidden message from the first pixel
pixelColor = bitmap.GetPixel(0,0);
messageLength = (pixelColor.R << 2) + (pixelColor.G << 1) + pixelColor.B;
messageStream = new MemoryStream(messageLength);
}else{

messageLength = (Int32)messageStream.Length;

if(messageStream.Length >= 16777215){ //The message is too long
String exceptionMessage = "The message is too long, only 16777215 bytes are allowed.";
throw new Exception(exceptionMessage);
}

//Check size of the carrier image

//Pixels available
long countPixels = (bitmapWidth*bitmapHeight) -1;
//Pixels required - start with one pixel for length of message
long countRequiredPixels = 1;
//add up the gaps between used pixels (sum up all the bytes of the key)
while((keyStream.Position < keyStream.Length)&&(keyStream.Position < messageLength)){
countRequiredPixels += keyStream.ReadByte();
}
//If the key is shorter than the message, it will be repeated again and again
//Multiply with count of key periods
countRequiredPixels *= (long)System.Math.Ceiling( ((float)messageStream.Length / (float)keyStream.Length) );

if(countRequiredPixels > countPixels){ //The bitmap is too small
String exceptionMessage = "The image is too small for this message and key. "+countRequiredPixels+" pixels are required.";
throw new Exception(exceptionMessage);
}

//Write length of the bitmap into the first pixel
int colorValue = messageLength;
int red = colorValue >> 2;
colorValue -= red << 2;
int green = colorValue >> 1;
int blue = colorValue - (green << 1);
pixelColor = Color.FromArgb(red, green, blue);
bitmap.SetPixel(0,0, pixelColor);
}

//Reset the streams
keyStream.Seek(0, SeekOrigin.Begin);
messageStream.Seek(0, SeekOrigin.Begin);

//Current position in the carrier bitmap
//Start with 1, because (0,0) contains the message's length
Point pixelPosition = new Point(1,0);

//Loop over the message and hide each byte
for(int messageIndex=0; messageIndex<messagelength; messageindex++){
=""
="" repeat="" the="" key,="" if="" it="" is="" shorter="" than="" message
="" if(keystream.position="=" keystream.length){
="" keystream.seek(0,="" seekorigin.begin);
="" }
="" get="" next="" pixel-count="" from="" use="" "1"="" it's="" 0
="" currentkeybyte="(byte)keyStream.ReadByte();
" currentstepwidth="(currentKeyByte==0)" ?="" (byte)1="" :="" currentkeybyte;

="" jump="" to="" reverse-read="" position="" and="" read="" end="" of="" stream
="" keyposition="keyStream.Position;
" keystream.seek(-keyposition,="" seekorigin.end);
="" currentreversekeybyte="(byte)keyStream.ReadByte();
" back="" normal="" position
="" keystream.seek(keyposition,="" perform="" line="" breaks,="" current="" step="" wider="" image
="" while(currentstepwidth=""> bitmapWidth){
currentStepWidth -= bitmapWidth;
pixelPosition.Y++;
}

//Move X-position
if((bitmapWidth - pixelPosition.X) < currentStepWidth){
pixelPosition.X = currentStepWidth - (bitmapWidth - pixelPosition.X);
pixelPosition.Y++;
}else{
pixelPosition.X += currentStepWidth;
}

//Get color of the "clean" pixel
pixelColor = bitmap.GetPixel(pixelPosition.X, pixelPosition.Y);

if(extract){
//Extract the hidden message-byte from the color
byte foundByte = (byte)(currentReverseKeyByte ^ GetColorComponent(pixelColor, currentColorComponent));
messageStream.WriteByte(foundByte);
//Rotate color components
currentColorComponent = (currentColorComponent==2) ? 0 : (currentColorComponent+1);

}else{
//To add a bit of confusion, xor the byte with a byte read from the keyStream
int currentByte = messageStream.ReadByte() ^ currentReverseKeyByte;

if(useGrayscale){
pixelColor = Color.FromArgb(currentByte, currentByte, currentByte);
}else{
//Change one component of the color to the message-byte
SetColorComponent(ref pixelColor, currentColorComponent, currentByte);
//Rotate color components
currentColorComponent = (currentColorComponent==2) ? 0 : (currentColorComponent+1);
}
bitmap.SetPixel(pixelPosition.X, pixelPosition.Y, pixelColor);
}
}

//the stream will be closed by the calling method
bitmap = null;
keyStream = null;
}

/// Return one component of a color
/// <param name="pixelColor" />The Color
/// <param name="colorComponent" />The component to return (0-R, 1-G, 2-B)
/// <returns>The requested component
private static byte GetColorComponent(Color pixelColor, int colorComponent){
byte returnValue = 0;
switch(colorComponent){
case 0:
returnValue = pixelColor.R;
break;
case 1:
returnValue = pixelColor.G;
break;
case 2:
returnValue = pixelColor.B;
break;
}
return returnValue;
}

/// Changees one component of a color
/// <param name="pixelColor" />The Color
/// <param name="colorComponent" />The component to change (0-R, 1-G, 2-B)
/// <param name="newValue" />New value of the component
private static void SetColorComponent(ref Color pixelColor, int colorComponent, int newValue){
switch(colorComponent){
case 0:
pixelColor = Color.FromArgb(newValue, pixelColor.G, pixelColor.B);
break;
case 1:
pixelColor = Color.FromArgb(pixelColor.R, newValue, pixelColor.B);
break;
case 2:
pixelColor = Color.FromArgb(pixelColor.R, pixelColor.G, newValue);
break;
}
}

private static String UnTrimColorString(String color, int desiredLength){
int difference = desiredLength - color.Length;
if(difference > 0){
color = new String('0', difference) + color;
}
return color;
}

}
}

AMAN ANWAR
AnswerRe: Help on the following Code in Steganography in Bitmaps Pin
Ravi Bhavnani9-Apr-06 6:04
professionalRavi Bhavnani9-Apr-06 6:04 
QuestionHow do I Remove Focus Cues on .NET Controls (selection outlines) Pin
Mikzi9-Apr-06 5:58
Mikzi9-Apr-06 5:58 
QuestionKey Press Event In C# Pin
Areff9-Apr-06 5:28
Areff9-Apr-06 5:28 
AnswerRe: Key Press Event In C# Pin
Jakob Farian Krarup9-Apr-06 6:16
Jakob Farian Krarup9-Apr-06 6:16 
AnswerRe: Key Press Event In C# Pin
Graham Nimbley9-Apr-06 14:46
Graham Nimbley9-Apr-06 14:46 
QuestionUnActivate C# Form Pin
Areff9-Apr-06 5:16
Areff9-Apr-06 5:16 
AnswerRe: UnActivate C# Form Pin
User 66589-Apr-06 7:19
User 66589-Apr-06 7:19 
QuestionForm to Web Service Pin
babamara9-Apr-06 4:42
babamara9-Apr-06 4:42 
AnswerRe: Form to Web Service Pin
babamara9-Apr-06 4:44
babamara9-Apr-06 4:44 
QuestionReflection Pin
rmedo9-Apr-06 4:29
rmedo9-Apr-06 4:29 
GeneralRe: Reflection Pin
Guffa9-Apr-06 5:32
Guffa9-Apr-06 5:32 
GeneralRe: Reflection Pin
rmedo10-Apr-06 22:26
rmedo10-Apr-06 22:26 
QuestionShowDialog Help Pin
rikkemus9-Apr-06 0:29
rikkemus9-Apr-06 0:29 
AnswerRe: ShowDialog Help Pin
WillemM9-Apr-06 2:45
WillemM9-Apr-06 2:45 
AnswerRe: ShowDialog Help Pin
CWIZO9-Apr-06 2:47
CWIZO9-Apr-06 2:47 
AnswerRe: ShowDialog Help Pin
Ravi Bhavnani9-Apr-06 5:16
professionalRavi Bhavnani9-Apr-06 5:16 
QuestionXml question Pin
eggie58-Apr-06 15:13
eggie58-Apr-06 15:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.