Advertisement
Valerian720

final

Apr 28th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.36 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.IO;
  4. using System.Text;
  5. using System.Windows.Forms;
  6.  
  7. namespace WindowsFormsApplication1
  8. {
  9.     public partial class Form1 : Form
  10.     {
  11.         public Form1()
  12.         {
  13.             InitializeComponent();
  14.         }
  15.  
  16.         private void Form1_Load(object sender, EventArgs e)
  17.         {
  18.  
  19.         }
  20.  
  21.         private void folderBrowserDialog1_HelpRequest(object sender, EventArgs e)
  22.         {
  23.  
  24.         }
  25.  
  26.         private void label1_Click(object sender, EventArgs e)
  27.         {
  28.  
  29.         }
  30.  
  31.         private void Form1_Load_1(object sender, EventArgs e)
  32.         {
  33.  
  34.         }
  35.  
  36.         public void button1_Click(object sender, EventArgs e)
  37.         {
  38.             string path = textBox1.Text;
  39.             converter.convert(path);
  40.         }
  41.  
  42.         public void textBox1_TextChanged(object sender, EventArgs e)
  43.         {
  44.  
  45.         }
  46.     }
  47.     public class converter
  48.     {
  49.      
  50.  
  51.  
  52.         public static void convert(string path)
  53.         {
  54.             //Вывод в ту же директорию, но .csv
  55.             string csv_path = path;
  56.             csv_path = csv_path.Replace("bmp", "csv");
  57.             Color[][] colorMatrix = GetBitMapColorMatrix(path, csv_path);
  58.             MessageBox.Show("Вывод успешно завершен в файлы " + csv_path + "  и  " + csv_path.Replace(".csv","_info.txt"));
  59.         }
  60.         //Функция получения ARGB каждого пикселя
  61.         public static Color[][] GetBitMapColorMatrix(string bitmapFilePath, string csv_path)
  62.         {
  63.             //writer
  64.             Encoding enc = Encoding.GetEncoding(1251);
  65.             FileStream output = new FileStream(csv_path, FileMode.Create);
  66.             StreamWriter writer = new StreamWriter(output, enc);
  67.             //Сам метод
  68.             Color[][] colorMatrix; //объявляем
  69.             using (var b1 = new Bitmap(bitmapFilePath))
  70.             {
  71.                 b1.RotateFlip(RotateFlipType.Rotate270FlipY);
  72.                 int hight = b1.Height;
  73.                 int width = b1.Width;
  74.                 colorMatrix = new Color[hight][]; //инициализируем
  75.                 for (int i = 0; i < hight; i++)
  76.                 {
  77.                     colorMatrix[i] = new Color[width];
  78.                     for (int j = 0; j < width; j++)
  79.                     {
  80.                         colorMatrix[i][j] = b1.GetPixel(i, j);
  81.                         writer.Write(colorMatrix[i][j] + ";"); //Тут же печатаем
  82.                     }
  83.                     writer.Write("\n");
  84.                 }
  85.                 writer.Close();
  86.                 string info_path = csv_path.Replace(".csv","_info.txt");
  87.                 string[] pixelFormat = b1.PixelFormat.ToString().Replace("Format", "").Replace("bpp", ";").Split(';');
  88.                 string[] info_contain = new string[3];info_contain[0] = "размер изоображения:" + b1.Height + "x" + b1.Width; info_contain[1] = "количество байт на пиксель:" + pixelFormat[0];info_contain[2] = "формат пикселей:"  + pixelFormat[1] ;
  89.                 File.WriteAllLines(info_path, info_contain, Encoding.UTF8);// создание документа по важным критериям  этого файла
  90.  
  91.             }
  92.             return colorMatrix;
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement