21 Mayıs 2012 Pazartesi

c# kodları kodbank

dosya açma:  openFileDialog kontrolü
using System.IO;



//openFileDialog1.InitialDirectory = "c:\\" ;
openFileDialog1.FileName = "";
openFileDialog1.Filter = "Image Files|*.jpg;*.gif;*.bmp;*.png;*.jpeg|All Files|*.*";
openFileDialog1.FilterIndex = 1;
openFileDialog1.RestoreDirectory = true;

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
 // istediğin kodu yaz




dosya okuma: mesela txt dosyası okuma

using System.IO;
string klasor_adi="yemek.txt";
string tarif="";
StreamReader sr = new StreamReader( klasor_adi );
tarif = sr.ReadToEnd();
sr.Close();

ya da

private string dosya_oku()
{
   string result,input;
   result = "";
   input = "";
   try
   {
     StreamReader re = File.OpenText(Server.MapPath("başlık.txt"));
    while ((input = re.ReadLine()) != null)
    {
       result += input;
    }
   re.Close();
   }
   catch (Exception e)
  {  }
 return result;
}

dosya adı klasör okuma:
using System.IO;
string foldername = "resim";
string filePath = Server.MapPath("~\\" + foldername + "/tn"); // asp.net uygulaması ise
DirectoryInfo di = new DirectoryInfo(filePath);
FileInfo[] fi = di.GetFiles("*.jpg");

string Split: bir string objesinin bir string kriterine göre ayırmak
string ayırac="<>";
string.Split(new string[] { "ayırac" }, StringSplitOptions.None);

dosyaya yazma:
StreamWriter sw = new StreamWriter("asd.htm");
sw.Write("deneme");
sw.Flush();
sw.Close();

tarih saat okuma:
//---yıl
string yil = DateTime.Today.Year.ToString();
//---ay
string ay = DateTime.Now.Month.ToString();
if (Convert.ToInt16(ay) < 10)
ay = "0" + ay;
//---gun
string gun = DateTime.Now.Day.ToString();
if (Convert.ToInt16(gun) < 10)
gun = "0" + gun;
//---saat
string saat = DateTime.Now.Hour.ToString();
if (Convert.ToInt16(saat) < 10)
saat = "0" + saat;
string dakika = DateTime.Now.Minute.ToString();
if (Convert.ToInt16( dakika ) < 10)
dakika = "0" + dakika ;
string saniye = DateTime.Now.Second.ToString();
if (Convert.ToInt16( saniye ) < 10)
saniye = "0" + saniye ;
//---------------------------

excel dosyası okuma:
using System.Data.OleDb;

protected void read_excel(string sheet)
{

string connString = "";// ConfigurationManager.ConnectionStrings["xls"].ConnectionString;
connString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + Server.MapPath("~\\excel\\ref.xls") + ";" +
"Extended Properties=Excel 8.0";
// Create the connection object
OleDbConnection oledbConn = new OleDbConnection(connString);
try
{
// Open connection
oledbConn.Open();

// Create OleDbCommand object and select data from worksheet Sheet1
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + sheet + "$]", oledbConn);

// Create new OleDbDataAdapter
OleDbDataAdapter oleda = new OleDbDataAdapter();

oleda.SelectCommand = cmd;

// Create a DataSet which will hold the data extracted from the worksheet.
DataSet ds = new DataSet();

// Fill the DataSet from the data extracted from the worksheet.
oleda.Fill(ds);

// Bind the data to the GridView
DataGrid1.DataSource = ds.Tables[0].DefaultView;
DataGrid1.DataBind();
}
catch (Exception ex)
{
//Label1.Text = ex.ToString();
}
}

rastgele sayı karakter oluşturma:


private int RandomNumber(int min, int max)   // rastgele sayı
{
Random random = new Random();
return random.Next(min, max);
}

private string RandomString(int size, bool lowerCase)    // rastgele karakter
{
StringBuilder builder = new StringBuilder();
Random random = new Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}

resim dosyasını yeniden boyutlandırmak:
private static Image resizeImage(Image imgToResize, Size size)
{
int sourceWidth = imgToResize.Width;
int sourceHeight = imgToResize.Height;

float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;

nPercentW = ((float)size.Width / (float)sourceWidth);
nPercentH = ((float)size.Height / (float)sourceHeight);

if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;

int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);

Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((Image)b);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();

return (Image)b;
}

resmi siyah beyaz hale getirmek:
private void im2bw()
{

try
{
Bitmap img = new Bitmap(pictureBox1.Image);
int ii = img.Width;

Color c;
for (int i = 0; i < img.Width; i++)
{
if (i % (ii / 10) == 0)
  //progressBar1.PerformStep();
for (int j = 0; j < img.Height; j++)
{
c = img.GetPixel(i, j);
int r = 0;
r = Convert.ToInt16(c.R);
int g = 0;
g = Convert.ToInt16(c.G);
int b = 0;
b = Convert.ToInt16(c.B);
int ans = (r + g + b) / 3;

  //hist[ans]++;    // histogram bilgisi için array kullanabilirsiniz.

  if (ans > 127)   // bu değeri histograma göre değiştirebilirsiniz
{
r = 255;
g = 255;
b = 255;
bits[i, j] = 1;
}
else
{
r = 0;
g = 0;
b = 0;
bits[i, j] = 0;
}

c = Color.FromArgb(r, g, b);
img.SetPixel(i, j, c);
}
}
  //pictureBox2.Image = img;
//MessageBox.Show("tamamlandı");
}
catch (Exception ex) { MessageBox.Show(ex.Message); }

}

resmin histogramını çıkarmak:
private void draw_hist()
{
Bitmap DArea = new Bitmap(pictureBox3.Width, pictureBox3.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
//clear_drawing2(DArea);
Graphics xGraph;

xGraph = Graphics.FromImage(DArea);

// Create solid brush.
Pen pen = new Pen(Color.Lime);
int x_scale = 1;
int y_scale = 1;
int hayt = pictureBox3.Height;
for (int i = 0; i < hist.Length; i++)
{
Point coord1 = new Point(i / x_scale, hayt - 0);
Point coord2 = new Point(i / x_scale, hayt - (hist[i] / y_scale));
xGraph.DrawLine(pen, coord1, coord2);
}
pictureBox3.Image = DArea;
xGraph.Dispose();
this.Invalidate();

}

grafik çizdirme:
private void drawGraph_log(int val)
{
Graphics g = pb1.CreateGraphics();
Pen p = new Pen(Color.LightGreen, 1);

if (graph_count == pb1.Width)
{
graph_count = 0;
g.Clear(Color.Black);
}
int ratio = Convert.ToInt32(nud_max_bat.Value * 100);
int x = 0;
int y = 0;
y = pb1.Height - (val * pb1.Height / ratio);

g.DrawLine(p, graph_count, pb1.Height, graph_count, y);

graph_count++;
g.Dispose();
this.Invalidate();
}

1 yorum:

  1. Resmin histogramını çıkarırken kullandığınız hist değişkenini nerede tanımladınız? Değişken içinde ne var?

    YanıtlaSil