Image Drag and Drop in WindowsApplication using C#.

Image Drag and Drop using C#

AllowDrop Property set to True in which control you have to drag the image.

For Ex,

Here is I'm going to drag an image from a picture box in to a panel.

Step 1:
Set AllowDrop property to True for panel control.

Step 2:

In picturebox mouse down event, write the code as follows,

pictureBox1 = (PictureBox)sender;
DoDragDrop(pictureBox1.Image, DragDropEffects.Copy);

Step 3:

In panel Drag_DragEnter event, write the following,

if (e.Data.GetDataPresent(typeof(Bitmap)))
e.Effect = DragDropEffects.Copy;
e.Effect = DragDropEffects.None;

Step 4:

In panel Drag_DragDrop event, write the following,

Panel pnlDroggedTheme = (Panel)sender;
pnlDroggedTheme.Height = panel1.Height;
pnlDroggedTheme.Width = panel1.Width;
pnlDroggedTheme.BackgroundImage = (Bitmap)e.Data.GetData(typeof(Bitmap));

Run the program, that's it...


Images from DB in ASP.NET

Here is sample coding for displaying image from DB(byte[] in DB).

public SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings(connectionstring);
public SqlCommand command;

protected void Button1_Click(object sender, EventArgs e)
command = new SqlCommand("select img_data from [tbl_image] where img_Id=1", connection);
byte[] imgData= (byte[])this.command.ExecuteScalar();
MemoryStream memoryStream = new MemoryStream();
memoryStream.Write(imgData, 0, imgData.Length);
System.Drawing.Image img = System.Drawing.Image.FromStream(memoryStream);
Response.ContentType = "image/Jpeg";
img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);


Image to ByteArray and ByteArray to Image using C#

Image to Byte[]

private static byte[] GetImageByteArr(System.Drawing.Image img)
byte[] ImgByte;
using (MemoryStream stream = new MemoryStream())
img.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
ImgByte = stream.ToArray();
return ImgByte;

Byte[] to Image

private Image bytetoimg(byte[] bytearr)
Image newImage;
MemoryStream ms=new MemoryStream(bytearr,0,bytearr.Length);
return newImage;


String to Byte Array And Byte Array to String.

Here is two methods for string to Byte[]

Method 1
public static byte[] StrToByteArray(string str)
System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding();
return encoding.GetBytes(str);

Method 2
byte[] byteArray = Encoding.UTF8.GetBytes(stringValue);

Byte[] to String

byte [] dBytes = ...
string str;
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
str = enc.GetString(dBytes);


System Tray Icon using C#

Making system tray icon for your application using c# is very easy.

Copy the following code in your form loading event.

private NotifyIcon appIcon = new NotifyIcon();

Icon ico = new Icon("icon.ico");
appIcon.Icon = ico;
appIcon.Text = "Vinoth Application";
appIcon.Visible = true;

put your icon file in your bin->Debug folder.

then run the application ...that's all.

How to list databases from DB?

Here is sample for listing the databases from DB (SQLServer) in ASP.NET:

string connectionString = "server=ServerName;uid=sa;pwd=pwd;";

using (SqlConnection connection = new SqlConnection(connectionString))


DataTable dt= connection.GetSchema("Databases");
foreach (DataRow row in dt.Rows)
Response.Output.WriteLine("Database: " + row["database_name"]);

WebClient Open Write using C#

Sample for WebClient Open Write using C#.

using System;
using System.IO;
using System.Net;
public class myTest

WebClient webClient = new WebClient();
string data = "upload data using webclient.";
Stream stream = webClient .OpenWrite(url);
StreamWriter sw = new StreamWriter(stream);

HTTPWebRequest - POST Method.

Sample for HTTPWebRequest using POST Method.

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("url")

request.Method = "POST";

string parameter = "";

parameter += "test=test1&";
parameter += "testing=testing1&";
parameter += "demo=demo1";

byte[] byteArray = Encoding.UTF8.GetBytes(parameter);

request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

if (HttpStatusCode.OK == response.StatusCode)
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
responseFromServer = reader.ReadToEnd();
File.WriteAllText(filepath, responseFromServer);

HTTPWebRequest using PUT method.

Sample for HTTPWebRequest using PUT Method.

string str = "test";
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] arr = encoding.GetBytes(str);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("url");
request.Method = "PUT";
request.ContentType = "text/plain";
request.ContentLength = arr.Length;
request.KeepAlive = true;
Stream dataStream = request.GetRequestStream();
dataStream.Write(arr, 0, arr.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string returnString = response.StatusCode.ToString();
catch {}

Email sending - ASP.NET

Sending email in ASP.NET using System.Net.Mail.

SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential("username", "password");//Here is your mail server's any username and password.
client.Host = "";// Your mail server's smtp host address.
client.Send("", "", "mail by Vinoth", "Test Mail");
catch (SmtpException ex)
Response.Write("SendEMail: " + ex.Message);