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;
}
else
{
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...
...S.VinothkumaR.
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);
connection.Open();
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);
connection.Close();
}
...S.VinothkumaR.
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);
connection.Open();
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);
connection.Close();
}
...S.VinothkumaR.
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);
ms.Write(bytearr,0,bytearr.Length);
newImage=Image.FromStream(ms,true);
return newImage;
}
...S.Vinothkumar
----------------
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);
ms.Write(bytearr,0,bytearr.Length);
newImage=Image.FromStream(ms,true);
return newImage;
}
...S.Vinothkumar
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);
...S.VinothkumaR.
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);
...S.VinothkumaR.
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.
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))
{
connection.Open();
DataTable dt= connection.GetSchema("Databases");
connection.Close();
foreach (DataRow row in dt.Rows)
{
Response.Output.WriteLine("Database: " + row["database_name"]);
}
}
string connectionString = "server=ServerName;uid=sa;pwd=pwd;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
DataTable dt= connection.GetSchema("Databases");
connection.Close();
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);
sw.WriteLine(data);
sw.Close();
stream.Close();
}
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);
sw.WriteLine(data);
sw.Close();
stream.Close();
}
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);
dataStream.Close();
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);
response.Close();
}
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);
dataStream.Close();
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);
response.Close();
}
HTTPWebRequest using PUT method.
Sample for HTTPWebRequest using PUT Method.
try
{
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);
dataStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string returnString = response.StatusCode.ToString();
}
catch {}
try
{
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);
dataStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string returnString = response.StatusCode.ToString();
}
catch {}
Email sending - ASP.NET
Sending email in ASP.NET using System.Net.Mail.
try
{
SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential("username", "password");//Here is your mail server's any username and password.
client.Host = "smtp.test.com";// Your mail server's smtp host address.
client.Send("fromAddress@test.com", "itvinoth83@gmail.com", "mail by Vinoth", "Test Mail");
}
catch (SmtpException ex)
{
Response.Write("SendEMail: " + ex.Message);
}
try
{
SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential("username", "password");//Here is your mail server's any username and password.
client.Host = "smtp.test.com";// Your mail server's smtp host address.
client.Send("fromAddress@test.com", "itvinoth83@gmail.com", "mail by Vinoth", "Test Mail");
}
catch (SmtpException ex)
{
Response.Write("SendEMail: " + ex.Message);
}
Subscribe to:
Posts (Atom)