|
CAN ANY one Help me out with Steganography In C#. it's Urgent ........
AMAN ANWAR
|
|
|
|
|
nwr_mn wrote: it's Urgent ........
What's the hurry? Most of us on CP are not in a hurry. Have you tried google.com[^]?
|
|
|
|
|
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
|
|
|
|
|
|
Selecting a tab in a tabcontrol leaves a selection outline around the tab name. I'm currently using a Event to shift the focus to a control on the tab but it still flashed the selection outline. I've also looked at the control ShowFocusCues property but it's read only. Is there an easy way to remove this behaviour using C#?
// Mikzi L. Freeman
|
|
|
|
|
Hi,
I want 2 use KeyEventHandler in a form but when i use this statement no event happen
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);
and
private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)<br />
{<br />
Lbl1.Text = ((System.Windows.Forms.Key) Sender).ToString();<br />
}
plz help me.
---------------------
Areff Bahrami(KAVEH)
Areff.HB@Gmail.com
---------------------
|
|
|
|
|
Have you tried setting the form's KeyPreview property to true?
Kind regards - Jakob
Three kinds of people in the world:
- Those who can count..
- Those who can't!
|
|
|
|
|
I think you may have confused what is passed in through 'sender'. In this situation sender returns a reference to the control which received the key press. If you want to access the key which was sent, you can use e.KeyChar
Graham
|
|
|
|
|
Hi,
I have tow form in my application and one button in Form1,
i want when button clicked Form2 be Show and Form1 be hide or UnActivate
---------------------
Areff Bahrami(KAVEH)
Areff.HB@Gmail.com
---------------------
|
|
|
|
|
this.Hide();
Form2 f = new Form2();
f.ShowDialog();
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Hi, all.
I want to convert this form to a web service but my knowladge is not enough. Please help.
using System;<br />
using System.Drawing;<br />
using System.Collections;<br />
using System.ComponentModel;<br />
using System.Windows.Forms;<br />
using System.Data;<br />
using System.Data.SqlClient;<br />
<br />
namespace AlbumViewer<br />
{<br />
public class MainForm : System.Windows.Forms.Form<br />
{<br />
private System.Windows.Forms.PictureBox pictureBox;<br />
private System.Windows.Forms.MainMenu mainMenu;<br />
private System.Windows.Forms.MenuItem menuItem3;<br />
private System.Windows.Forms.TreeView treeAlbum;<br />
private System.Windows.Forms.MenuItem mnuFile;<br />
private System.Windows.Forms.MenuItem mnuOpen;<br />
private System.Windows.Forms.MenuItem mnuExit;<br />
private System.Windows.Forms.MenuItem mnuOptions;<br />
private System.Windows.Forms.MenuItem mnuSlideShow;<br />
private System.Data.SqlClient.SqlConnection sqlConn;<br />
<br />
private System.Windows.Forms.OpenFileDialog FileOpenDlg;<br />
private System.Windows.Forms.MenuItem mnuAddPhoto;<br />
private System.Windows.Forms.MenuItem mnuRenameAlbum;<br />
private System.Windows.Forms.MenuItem mnuDeleteAlbum;<br />
private System.Windows.Forms.ContextMenu mnuPhotoPopup;<br />
private System.Windows.Forms.MenuItem mnuRenamePhoto;<br />
private System.Windows.Forms.MenuItem mnuDeletePhoto;<br />
private System.Windows.Forms.ContextMenu mnuAlbumPopup;<br />
private System.Windows.Forms.MenuItem mnuNewAlbum;<br />
private System.Windows.Forms.TextBox txtDesc;<br />
private System.Windows.Forms.Button btnUpdate;<br />
private System.Windows.Forms.Label lblDesc;<br />
private System.Windows.Forms.MenuItem mnuEditDesc;<br />
private System.Windows.Forms.MenuItem mnuAlbumEdit;<br />
private System.Windows.Forms.MenuItem mnuPhotoEdit;<br />
private System.Windows.Forms.ContextMenu mnuImage;<br />
private System.Windows.Forms.MenuItem menuItem1;<br />
private System.Windows.Forms.MenuItem menuItem2;<br />
<br />
private System.ComponentModel.Container components = null;<br />
<br />
[STAThread]<br />
static void Main() <br />
{<br />
Application.Run(new MainForm());<br />
}<br />
<br />
<br />
public MainForm()<br />
{<br />
InitializeComponent();<br />
<br />
LoadTree();<br />
}<br />
<br />
<br />
protected override void Dispose( bool disposing )<br />
{<br />
if( disposing )<br />
{<br />
if (components != null) <br />
{<br />
components.Dispose();<br />
}<br />
}<br />
base.Dispose( disposing );<br />
}<br />
<br />
<br />
private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)<br />
{<br />
if(sqlConn.State == ConnectionState.Open)<br />
sqlConn.Close();<br />
}<br />
<br />
<br />
private void LoadTree()<br />
{<br />
try<br />
{<br />
treeAlbum.Nodes.Clear();<br />
<br />
if(sqlConn.State == ConnectionState.Closed)<br />
sqlConn.Open();<br />
<br />
if(sqlConn.State == ConnectionState.Open)<br />
{<br />
SqlCommand sqlCmd = new SqlCommand("sp_GetPhotoAlbums", sqlConn);<br />
sqlCmd.CommandType = CommandType.StoredProcedure;<br />
<br />
SqlDataReader sqlPhotoAlbum = sqlCmd.ExecuteReader();<br />
<br />
int nAlbumID = 0;
int nAlbumIndex = -1;
<br />
while( sqlPhotoAlbum.Read() )<br />
{<br />
if( nAlbumID != (int)sqlPhotoAlbum["AlbumID"] )<br />
{<br />
InsertAlbum(sqlPhotoAlbum["Album"].ToString(), (int)sqlPhotoAlbum["AlbumID"], sqlPhotoAlbum["Album_Desc"].ToString());<br />
nAlbumID = (int)sqlPhotoAlbum["AlbumID"];<br />
nAlbumIndex++;<br />
}<br />
InsertPhoto(nAlbumIndex, sqlPhotoAlbum["Photo"].ToString(), (int)sqlPhotoAlbum["PhotoID"], sqlPhotoAlbum["Photo_Desc"].ToString());<br />
}<br />
sqlPhotoAlbum.Close();<br />
}<br />
}<br />
catch(SqlException e)<br />
{<br />
MessageBox.Show(e.Message);<br />
}<br />
}<br />
<br />
<br />
private void InsertAlbum(string strName, int nAlbum, string strDesc)<br />
{<br />
try<br />
{<br />
TreeNode node = new TreeNode(strName);<br />
node.Tag = new TreeItem(ItemType.Album, nAlbum, strDesc);<br />
<br />
treeAlbum.Nodes.Add(node); <br />
}<br />
catch(Exception e)<br />
{<br />
MessageBox.Show(e.Message);<br />
}<br />
}<br />
<br />
<br />
private void InsertPhoto(int nAlbum, string strName, int nPhoto, string strDesc)<br />
{<br />
try<br />
{<br />
TreeNode node = new TreeNode(strName);<br />
node.Tag = new TreeItem(ItemType.Photo, nPhoto, strDesc);;<br />
<br />
treeAlbum.Nodes[nAlbum].Nodes.Add(node);<br />
}<br />
catch(Exception e)<br />
{<br />
MessageBox.Show(e.Message);<br />
}<br />
}<br />
<br />
<br />
private void InsertImage(ref byte[] buffer, string strName, int nAlbum)<br />
{<br />
try<br />
{<br />
SqlCommand cmd = new SqlCommand("sp_InsertPhoto", sqlConn);<br />
cmd.CommandType = CommandType.StoredProcedure;<br />
<br />
SqlParameter param = cmd.Parameters.Add("RETURN_VALUE", SqlDbType.Int);<br />
param.Direction = ParameterDirection.ReturnValue;<br />
<br />
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = strName;<br />
cmd.Parameters.Add("@image", SqlDbType.Image).Value = buffer;<br />
cmd.Parameters.Add("@album", SqlDbType.Int).Value = nAlbum;<br />
<br />
cmd.ExecuteNonQuery();<br />
<br />
int nID = (int)cmd.Parameters["RETURN_VALUE"].Value;<br />
<br />
TreeNode node = new TreeNode(strName);<br />
node.Tag = new TreeItem(ItemType.Photo, nID, "Enter description");;<br />
<br />
nID = treeAlbum.SelectedNode.Index;<br />
treeAlbum.Nodes[nID].Nodes.Add(node);<br />
}<br />
catch(Exception e)<br />
{<br />
MessageBox.Show(e.Message);<br />
}<br />
}<br />
<br />
<br />
#region Main menu handlers<br />
<br />
private void OnExit(object sender, System.EventArgs e)<br />
{<br />
Close();<br />
}<br />
<br />
<br />
private void OnNewAlbum(object sender, System.EventArgs e)<br />
{<br />
try<br />
{<br />
SqlCommand cmd = new SqlCommand("sp_NewAlbum", sqlConn);<br />
cmd.CommandType = CommandType.StoredProcedure;<br />
<br />
SqlParameter param = cmd.Parameters.Add("RETURN_VALUE", SqlDbType.Int);<br />
param.Direction = ParameterDirection.ReturnValue;<br />
<br />
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = "New Album";<br />
<br />
cmd.ExecuteNonQuery();<br />
<br />
int nID = (int)cmd.Parameters["RETURN_VALUE"].Value;<br />
<br />
TreeNode node = new TreeNode("New Album");<br />
node.Tag = new TreeItem(ItemType.Album, nID, "Enter description"); <br />
treeAlbum.Nodes.Add(node); <br />
}<br />
catch(Exception ex)<br />
{<br />
MessageBox.Show(ex.Message);<br />
} <br />
}<br />
<br />
#endregion<br />
<br />
#region Treeview handlers<br />
<br />
private void OnMouseUp(object sender, System.Windows.Forms.MouseEventArgs e)<br />
{<br />
if( e.Button == MouseButtons.Right )<br />
{<br />
TreeNode node = treeAlbum.GetNodeAt(e.X, e.Y);<br />
if(node != null)<br />
{ <br />
treeAlbum.SelectedNode = node;<br />
<br />
TreeItem item = (TreeItem)node.Tag;<br />
if( ItemType.Album == item.Type )<br />
mnuAlbumPopup.Show(treeAlbum, new Point(e.X, e.Y)); <br />
else if( ItemType.Photo == item.Type )<br />
mnuPhotoPopup.Show(treeAlbum, new Point(e.X, e.Y)); <br />
}<br />
}<br />
}<br />
<br />
<br />
private void OnAfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)<br />
{<br />
try<br />
{<br />
if( true == btnUpdate.Visible )<br />
{<br />
btnUpdate.Visible = false;<br />
txtDesc.Visible = false;<br />
lblDesc.Visible = true;<br />
}<br />
<br />
TreeItem item = (TreeItem)e.Node.Tag;<br />
<br />
if( ItemType.Album == item.Type )<br />
{<br />
lblDesc.Text = item.Desc;
pictureBox.Image = null;
return;<br />
}<br />
<br />
ShowPhoto(item);<br />
}<br />
catch(Exception ex)<br />
{<br />
MessageBox.Show(ex.Message);<br />
}<br />
}<br />
<br />
private void OnAfterLabelEdit(object sender, System.Windows.Forms.NodeLabelEditEventArgs e)<br />
{<br />
try<br />
{<br />
TreeItem item = (TreeItem)treeAlbum.SelectedNode.Tag;<br />
<br />
string strCmd;<br />
if( ItemType.Album == item.Type )<br />
strCmd = String.Format("UPDATE Albums SET name = '{0}' WHERE id = {1}", e.Label, item.Id);<br />
else<br />
strCmd = String.Format("UPDATE Photos SET name = '{0}' WHERE id = {1}", e.Label, item.Id);<br />
<br />
SqlCommand cmd = new SqlCommand(strCmd, sqlConn);<br />
cmd.ExecuteNonQuery();<br />
}<br />
catch(Exception ex)<br />
{<br />
MessageBox.Show(ex.Message);<br />
} <br />
}<br />
<br />
<br />
private void OnKeyUp(object sender, System.Windows.Forms.KeyEventArgs e)<br />
{<br />
if( Keys.F2 == e.KeyCode )<br />
treeAlbum.SelectedNode.BeginEdit();<br />
}<br />
<br />
#endregion<br />
<br />
#region Context menu handlers<br />
private void OnRename(object sender, System.EventArgs e)<br />
{<br />
treeAlbum.SelectedNode.BeginEdit();<br />
}<br />
<br />
<br />
private void OnAddPhoto(object sender, System.EventArgs e)<br />
{<br />
if( DialogResult.OK == FileOpenDlg.ShowDialog() )<br />
{<br />
TreeItem item = (TreeItem)treeAlbum.SelectedNode.Tag;<br />
<br />
foreach( string file in FileOpenDlg.FileNames )<br />
{<br />
System.IO.FileStream stream = new System.IO.FileStream(file, System.IO.FileMode.Open, System.IO.FileAccess.Read);<br />
byte[] buffer = new byte[stream.Length];<br />
stream.Read(buffer, 0, (int)stream.Length);<br />
stream.Close();<br />
<br />
string strName = System.IO.Path.GetFileNameWithoutExtension(file);<br />
<br />
InsertImage(ref buffer, strName, item.Id);<br />
buffer = null;<br />
}<br />
treeAlbum.SelectedNode = treeAlbum.SelectedNode.LastNode;<br />
}<br />
}<br />
<br />
private void OnDelete(object sender, System.EventArgs e)<br />
{<br />
try<br />
{<br />
TreeItem item = (TreeItem)treeAlbum.SelectedNode.Tag;<br />
<br />
int nIndex = treeAlbum.SelectedNode.Index;<br />
<br />
string strCmd;<br />
if( ItemType.Album == item.Type )<br />
strCmd = String.Format("DELETE FROM Albums WHERE id = {0}", item.Id);<br />
else<br />
strCmd = String.Format("DELETE FROM Photos WHERE id = {0}", item.Id);<br />
<br />
SqlCommand cmd = new SqlCommand(strCmd, sqlConn);<br />
cmd.ExecuteNonQuery();<br />
<br />
TreeNode node = null;<br />
switch(item.Type)<br />
{<br />
case ItemType.Photo:<br />
node = treeAlbum.SelectedNode.NextNode;<br />
if( null == node )<br />
node = treeAlbum.SelectedNode.PrevNode;<br />
<br />
if( null == node )<br />
node = treeAlbum.SelectedNode.Parent;<br />
<br />
treeAlbum.Nodes[treeAlbum.SelectedNode.Parent.Index].Nodes.RemoveAt(nIndex);<br />
break;<br />
<br />
case ItemType.Album:<br />
node = treeAlbum.SelectedNode.NextNode;<br />
if( null == node )<br />
node = treeAlbum.SelectedNode.PrevNode;<br />
<br />
pictureBox.Image = null;<br />
<br />
treeAlbum.Nodes.RemoveAt(nIndex);<br />
break;<br />
}<br />
<br />
if( null != node )<br />
treeAlbum.SelectedNode = node;<br />
}<br />
catch(Exception ex)<br />
{<br />
MessageBox.Show(ex.Message);<br />
} <br />
}<br />
<br />
<br />
private void OnEditDesc(object sender, System.EventArgs e)<br />
{<br />
btnUpdate.Visible = true;<br />
txtDesc.Visible = true;<br />
lblDesc.Visible = false;<br />
<br />
txtDesc.Text = lblDesc.Text;<br />
}<br />
<br />
#endregion<br />
<br />
private void ShowPhoto(TreeItem item)<br />
{<br />
try<br />
{<br />
string strCmd = String.Format("SELECT photo FROM Photos WHERE id = {0}", item.Id);<br />
SqlCommand cmd = new SqlCommand(strCmd, sqlConn);<br />
<br />
lblDesc.Text = item.Desc;<br />
<br />
byte[] b = (byte[])cmd.ExecuteScalar();<br />
if(b.Length > 0)<br />
{<br />
System.IO.MemoryStream stream = new System.IO.MemoryStream(b, true);<br />
stream.Write(b, 0, b.Length);<br />
<br />
DrawToScale(new Bitmap(stream));<br />
<br />
stream.Close();<br />
}<br />
}<br />
catch(Exception e)<br />
{<br />
MessageBox.Show(e.Message);<br />
}<br />
}<br />
<br />
private void OnUpdate(object sender, System.EventArgs e)<br />
{<br />
try<br />
{<br />
TreeItem item = (TreeItem)treeAlbum.SelectedNode.Tag;<br />
<br />
string strCmd;<br />
if( ItemType.Album == item.Type )<br />
strCmd = String.Format("UPDATE Albums SET [desc] = '{0}' WHERE id = {1}", txtDesc.Text, item.Id);<br />
else<br />
strCmd = String.Format("UPDATE Photos SET [desc] = '{0}' WHERE id = {1}", txtDesc.Text, item.Id);<br />
<br />
SqlCommand cmd = new SqlCommand(strCmd, sqlConn);<br />
cmd.ExecuteNonQuery();<br />
<br />
item.Desc = txtDesc.Text;<br />
<br />
lblDesc.Text = txtDesc.Text;<br />
<br />
btnUpdate.Visible = false;<br />
txtDesc.Visible = false;<br />
lblDesc.Visible = true;<br />
}<br />
catch(Exception ex)<br />
{<br />
MessageBox.Show(ex.Message);<br />
} <br />
}<br />
<br />
private void DrawToScale(Image bmp)<br />
{<br />
Rectangle rc = pictureBox.ClientRectangle;<br />
<br />
SizeF size = new SizeF( bmp.Width / bmp.HorizontalResolution, bmp.Height / bmp.VerticalResolution);<br />
float fScale = Math.Min( rc.Width / size.Width, rc.Height / size.Height);<br />
<br />
size.Width *= fScale;<br />
size.Height *= fScale;<br />
<br />
pictureBox.Image = new Bitmap(bmp, size.ToSize());<br />
}<br />
<br />
#region Windows Form Designer generated code<br />
private void InitializeComponent()<br />
{<br />
this.treeAlbum = new System.Windows.Forms.TreeView();<br />
this.mnuAlbumPopup = new System.Windows.Forms.ContextMenu();<br />
this.mnuAddPhoto = new System.Windows.Forms.MenuItem();<br />
this.mnuAlbumEdit = new System.Windows.Forms.MenuItem();<br />
this.mnuRenameAlbum = new System.Windows.Forms.MenuItem();<br />
this.mnuDeleteAlbum = new System.Windows.Forms.MenuItem();<br />
this.pictureBox = new System.Windows.Forms.PictureBox();<br />
this.mnuImage = new System.Windows.Forms.ContextMenu();<br />
this.menuItem1 = new System.Windows.Forms.MenuItem();<br />
this.menuItem2 = new System.Windows.Forms.MenuItem();<br />
this.mainMenu = new System.Windows.Forms.MainMenu();<br />
this.mnuFile = new System.Windows.Forms.MenuItem();<br />
this.mnuOpen = new System.Windows.Forms.MenuItem();<br />
this.mnuNewAlbum = new System.Windows.Forms.MenuItem();<br />
this.menuItem3 = new System.Windows.Forms.MenuItem();<br />
this.mnuExit = new System.Windows.Forms.MenuItem();<br />
this.mnuOptions = new System.Windows.Forms.MenuItem();<br />
this.mnuSlideShow = new System.Windows.Forms.MenuItem();<br />
this.mnuEditDesc = new System.Windows.Forms.MenuItem();<br />
this.sqlConn = new System.Data.SqlClient.SqlConnection();<br />
this.FileOpenDlg = new System.Windows.Forms.OpenFileDialog();<br />
this.mnuPhotoPopup = new System.Windows.Forms.ContextMenu();<br />
this.mnuRenamePhoto = new System.Windows.Forms.MenuItem();<br />
this.mnuPhotoEdit = new System.Windows.Forms.MenuItem();<br />
this.mnuDeletePhoto = new System.Windows.Forms.MenuItem();<br />
this.txtDesc = new System.Windows.Forms.TextBox();<br />
this.btnUpdate = new System.Windows.Forms.Button();<br />
this.lblDesc = new System.Windows.Forms.Label();<br />
this.SuspendLayout();<br />
this.treeAlbum.ImageIndex = -1;<br />
this.treeAlbum.LabelEdit = true;<br />
this.treeAlbum.Name = "treeAlbum";<br />
this.treeAlbum.SelectedImageIndex = -1;<br />
this.treeAlbum.Size = new System.Drawing.Size(224, 504);<br />
this.treeAlbum.TabIndex = 0;<br />
this.treeAlbum.MouseUp += new System.Windows.Forms.MouseEventHandler(this.OnMouseUp);<br />
this.treeAlbum.KeyUp += new System.Windows.Forms.KeyEventHandler(this.OnKeyUp);<br />
this.treeAlbum.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.OnAfterSelect);<br />
this.treeAlbum.AfterLabelEdit += new System.Windows.Forms.NodeLabelEditEventHandler(this.OnAfterLabelEdit);<br />
this.mnuAlbumPopup.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {<br />
this.mnuAddPhoto,<br />
this.mnuAlbumEdit,<br />
this.mnuRenameAlbum,<br />
this.mnuDeleteAlbum});<br />
this.mnuAddPhoto.Index = 0;<br />
this.mnuAddPhoto.Text = "Add Photo";<br />
this.mnuAddPhoto.Click += new System.EventHandler(this.OnAddPhoto);<br />
this.mnuAlbumEdit.Index = 1;<br />
this.mnuAlbumEdit.Text = "Edit Description";<br />
this.mnuAlbumEdit.Click += new System.EventHandler(this.OnEditDesc);<br />
this.mnuRenameAlbum.Index = 2;<br />
this.mnuRenameAlbum.Text = "Rename";<br />
this.mnuRenameAlbum.Click += new System.EventHandler(this.OnRename);<br />
this.mnuDeleteAlbum.Index = 3;<br />
this.mnuDeleteAlbum.Text = "Delete";<br />
this.mnuDeleteAlbum.Click += new System.EventHandler(this.OnDelete);<br />
this.pictureBox.ContextMenu = this.mnuImage;<br />
this.pictureBox.Location = new System.Drawing.Point(232, 0);<br />
this.pictureBox.Name = "pictureBox";<br />
this.pictureBox.Size = new System.Drawing.Size(500, 300);<br />
this.pictureBox.TabIndex = 1;<br />
this.pictureBox.TabStop = false;<br />
this.mnuImage.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {<br />
this.menuItem1,<br />
this.menuItem2});<br />
this.menuItem1.Index = 0;<br />
this.menuItem1.Text = "Rotate Left 90";<br />
this.menuItem1.Click += new System.EventHandler(this.OnRotateLeft);<br />
this.menuItem2.Index = 1;<br />
this.menuItem2.Text = "Rotate Right 90";<br />
this.menuItem2.Click += new System.EventHandler(this.OnRotateRight);<br />
this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {<br />
this.mnuFile,<br />
this.mnuOptions});<br />
this.mnuFile.Index = 0;<br />
this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {<br />
this.mnuOpen,<br />
this.mnuNewAlbum,<br />
this.menuItem3,<br />
this.mnuExit});<br />
this.mnuFile.Text = "File";<br />
this.mnuOpen.Index = 0;<br />
this.mnuOpen.Text = "Open";<br />
this.mnuNewAlbum.Index = 1;<br />
this.mnuNewAlbum.Text = "New Album";<br />
this.mnuNewAlbum.Click += new System.EventHandler(this.OnNewAlbum);<br />
this.menuItem3.Index = 2;<br />
this.menuItem3.Text = "-";<br />
this.mnuExit.Index = 3;<br />
this.mnuExit.Text = "E&xit";<br />
this.mnuExit.Click += new System.EventHandler(this.OnExit);<br />
this.mnuOptions.Index = 1;<br />
this.mnuOptions.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {<br />
this.mnuSlideShow,<br />
this.mnuEditDesc});<br />
this.mnuOptions.Text = "Options";<br />
this.mnuSlideShow.Index = 0;<br />
this.mnuSlideShow.Text = "Slide Show";<br />
this.mnuEditDesc.Index = 1;<br />
this.mnuEditDesc.Text = "Edit Description";<br />
this.mnuEditDesc.Click += new System.EventHandler(this.OnEditDesc);<br />
this.sqlConn.ConnectionString = "data source=HOMESVR;initial catalog=Photos;integrated security=SSPI;persist secur" +<br />
"ity info=False;workstation id=DAD;packet size=4096";<br />
this.FileOpenDlg.Filter = "Gif files|*.gif|Jpeg Files|*.jpg;*.jpeg|Bmp Files|*.bmp";<br />
this.FileOpenDlg.Multiselect = true;<br />
this.FileOpenDlg.Title = "Select Photos";<br />
this.mnuPhotoPopup.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {<br />
this.mnuRenamePhoto,<br />
this.mnuPhotoEdit,<br />
this.mnuDeletePhoto});<br />
this.mnuRenamePhoto.Index = 0;<br />
this.mnuRenamePhoto.Text = "Rename";<br />
this.mnuRenamePhoto.Click += new System.EventHandler(this.OnRename);<br />
this.mnuPhotoEdit.Index = 1;<br />
this.mnuPhotoEdit.Text = "Edit Description";<br />
this.mnuPhotoEdit.Click += new System.EventHandler(this.OnEditDesc);<br />
this.mnuDeletePhoto.Index = 2;<br />
this.mnuDeletePhoto.Text = "Delete";<br />
this.mnuDeletePhoto.Click += new System.EventHandler(this.OnDelete);<br />
this.txtDesc.Location = new System.Drawing.Point(232, 312);<br />
this.txtDesc.MaxLength = 200;<br />
this.txtDesc.Multiline = true;<br />
this.txtDesc.Name = "txtDesc";<br />
this.txtDesc.Size = new System.Drawing.Size(504, 112);<br />
this.txtDesc.TabIndex = 2;<br />
this.txtDesc.Text = "";<br />
this.txtDesc.Visible = false;<br />
this.btnUpdate.Location = new System.Drawing.Point(656, 432);<br />
this.btnUpdate.Name = "btnUpdate";<br />
this.btnUpdate.TabIndex = 3;<br />
this.btnUpdate.Text = "Update";<br />
this.btnUpdate.Visible = false;<br />
this.btnUpdate.Click += new System.EventHandler(this.OnUpdate);<br />
this.lblDesc.Location = new System.Drawing.Point(232, 312);<br />
this.lblDesc.Name = "lblDesc";<br />
this.lblDesc.Size = new System.Drawing.Size(504, 112);<br />
this.lblDesc.TabIndex = 4;<br />
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);<br />
this.ClientSize = new System.Drawing.Size(792, 566);<br />
this.Controls.AddRange(new System.Windows.Forms.Control[] {<br />
this.lblDesc,<br />
this.btnUpdate,<br />
this.txtDesc,<br />
this.pictureBox,<br />
this.treeAlbum});<br />
this.Menu = this.mainMenu;<br />
this.Name = "MainForm";<br />
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;<br />
this.Text = "Album Viewer";<br />
this.Closing += new System.ComponentModel.CancelEventHandler(this.MainForm_Closing);<br />
this.ResumeLayout(false);<br />
<br />
}<br />
#endregion<br />
<br />
#region PictureBox context menu handlers<br />
private void OnRotateLeft(object sender, System.EventArgs e)<br />
{<br />
try<br />
{<br />
Image img = pictureBox.Image;<br />
img.RotateFlip(RotateFlipType.Rotate270FlipNone);<br />
DrawToScale(img);<br />
}<br />
catch(Exception ex)<br />
{<br />
MessageBox.Show(ex.Message);<br />
}<br />
}<br />
<br />
private void OnRotateRight(object sender, System.EventArgs e)<br />
{<br />
try<br />
{<br />
Image img = pictureBox.Image;<br />
img.RotateFlip(RotateFlipType.Rotate90FlipNone);<br />
DrawToScale(img);<br />
}<br />
catch(Exception ex)<br />
{<br />
MessageBox.Show(ex.Message);<br />
}<br />
}<br />
<br />
#endregion<br />
}<br />
}
|
|
|
|
|
Is there some convertor or good article which can help me.
|
|
|
|
|
Hi all,
I want to make component that have two public methods, One that reflects the public exposed methods and classes of any selected .NET component and another that is passed all necessary parameters to run another method in another .Net component and return the ruselt to the caller.
I hope any one can help me,
Thanks
|
|
|
|
|
In order for someone to help you, you have to tell what you need help with.
Make a component? Create methods? Make methods public? Using reflection? Return a result?
---
b { font-weight: normal; }
|
|
|
|
|
I want to know how to make com component, how to use reflection and hoe to return result through reflection.
Thanks for help
|
|
|
|
|
Im making a window with 3 fields that needs to be filled out. Im using it as a modal window.
2 buttons for OK and Cancel.
If the User presses OK, i want to inform him, he hasnt filled out all fields, if thats the case. And then keep the window open.
The question is, how do i suppress the form from closing, when not every field is filled out.
Thx in advance.
|
|
|
|
|
Don't fill in the DialogResult property of the Ok button, but set the dialogresult of the dialog using the eventhandler of the Ok button.
private void okButton_Click(object sender,EventArgs e) {
if(textField1.Text != "") {
DialogResult = DialogResult.OK;
}
}
Simple as that, if the textfield isn't filled, the dialogbox won't close.
You can modify this to show a messagebox with the exact error.
WM.
What about weapons of mass-construction?
|
|
|
|
|
Write a event hanlder for the Closing event.
Inthere you check if the user has filled all the values and if he his not you set the Cancel property of the argument e to true:
e.Cancel = true;
--------------------------------------------------------
My development blog
Q:What does the derived class in C# tell to it's parent?
A:All your base are belong to us!
|
|
|
|
|
Imho, this is best done using the ErrorProvider [^] component. It's a nice alternative to displaying a message box.
/ravi
My new year's resolution: 2048 x 1536
Home | Music | Articles | Freeware | Trips
ravib(at)ravib(dot)com
|
|
|
|
|
So I want to call the xpath function count(papers/id) on an XmlNode.
I try to call SelectSingleNode, but that throws an exception because count doesn't find xml nodes, it returns how many nodes there are.
How can I use the xpath count function on an XmlNode???
/\ |_ E X E GG
|
|
|
|
|
Why not use the SelectNodes function to get a set of nodes, and then use the Count property to see how many are returned?
int noOfIdNodes = theNode.SelectNodes("papers/id").Count
Kind regards - Jakob
Three kinds of people in the world:
- Those who can count..
- Those who can't!
|
|
|
|
|
yeahm this what I eventually figured out, thanks.
/\ |_ E X E GG
|
|
|
|
|
in some cases i want to get the errors number from sqlServer in C#, but i dont know how to do it.
please show me the way to catch it.
Thanks alot.
superdragon
|
|
|
|
|
try
{
}
catch (SqlException ex)
{
int errorCode = ex.ErrorCode;
}
Is that what you needed
|
|
|
|
|
Thanks for your helpings. Now i can catch it with your instruction
superdragon
|
|
|
|
|