Machine Imagination Technologies
マシン 想像力 テクノロジー

 

Useful Sample C# Codes

 

loadDataToList ============
private void loadDataToList(string filename, List<string> list, bool ignoreFirstLine)
{
    list.Clear();
    try
    {
        StreamReader sr = new System.IO.StreamReader(filename);
        string line = "";
        if (ignoreFirstLine)
        line = sr.ReadLine();

        while ((line = sr.ReadLine()) != null)
        {
            list.Add(line);
        }
    }
    catch
    {
        //something
    }
}
============
StreamReader ============
using (StreamReader sr = new StreamReader(filename))
{

    string line = "";
    while ((line = sr.ReadLine()) != null)
    {

        list.Add(line);
    }

}
============
StreamWriter ============
StreamWriter sw = new System.IO.StreamWriter(indexIndexAllCorrectedFile);

for (int j = 0; j < Flist.Count; j++)
    sw.WriteLine(list[j]);
sw.Close()
============
StreamWriter ============
using (StreamWriter sw = new StreamWriter(fname))
{

    for (int i = 0; i < list.Count; i++)
        sw.WriteLine(list[i]);
}
============
StreamWriter ============
void printBuffer(string fname)
{
    using (StreamWriter sw = new StreamWriter(fname))
    {
       for (int j = 0; j < img_height; j++)
        {
            string line = "";
            for (int i = 0; i < img_width; i++)
                line += outImgData[j * img_width + i].ToString() + ",";
            sw.WriteLine(line);
        }
    }
}
============
Convert8to24Image ============
private void Convert8to24Image(Bitmap bmpSource, Bitmap bmpNew)
{
    // copy image
    BitmapData dataSource = bmpSource.LockBits(new Rectangle(0, 0, bmpSource.Width, bmpSource.Height),
    ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
    BitmapData dataNew = bmpNew.LockBits(new Rectangle(0, 0, bmpNew.Width, bmpNew.Height),
    ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
    int i, j;
    unsafe
    {
        int pos_new;
        byte* pSource = (byte*)(dataSource.Scan0);
        byte* p_new = (byte*)(dataNew.Scan0);
        for (i = 0; i < dataNew.Height; i++)
        {
            for (j = 0; j < dataNew.Width; j++)
            {
                pos_new = 3 * j + i * dataNew.Stride;
                p_new[pos_new] = p_new[pos_new + 1] = p_new[pos_new + 2] = pSource[j + i * dataSource.Stride];
            }
        }
    }
    bmpSource.UnlockBits(dataSource);
    bmpNew.UnlockBits(dataNew);
}
============
Convert24to8Image ============
private void Convert24to8Image(Bitmap bmpSource, Bitmap bmpNew)
{
    // copy image
    BitmapData data24 = bmpSource.LockBits(new Rectangle(0, 0, bmpSource.Width, bmpSource.Height),
    ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
    BitmapData data8 = bmpNew.LockBits(new Rectangle(0, 0, bmpNew.Width, bmpNew.Height),
    ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
    int i, j;
    unsafe
    {
        int pos24, pos8;
        byte* p24 = (byte*)(data24.Scan0);
        byte* p8 = (byte*)(data8.Scan0);
        for (i = 0; i < data8.Height; i++)
        {
            for (j = 0; j < data8.Width; j++)
            {
                pos8 = j + i * data8.Stride;
                pos24 = 3 * j + i * data24.Stride;
                p8[pos8] = (p24[pos24] + p24[pos24 + 1] + p24[pos24 + 2])/3;
            }
        }
    }
    bmpSource.UnlockBits(data24);
    bmpNew.UnlockBits(data8);
}
============
   
   

 

 

 

Copyright©2016 Machine Imagination Technologies. All rights reserved.