Advertisement
Manu404

PicToHtml

Jun 12th, 2011
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.IO;
  5.  
  6. namespace PicToHtml
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Create("mitsudomoe.jpeg");
  13.             Console.ReadKey();
  14.         }
  15.  
  16.         private static void Create(string imgPath)
  17.         {
  18.             List<string> codes = new List<string>();
  19.             Bitmap img = new Bitmap(imgPath);
  20.             int l = img.Width;
  21.             int h = img.Height;
  22.             TextWriter tw = CreatePage(imgPath + ".html", imgPath);
  23.  
  24.             for (int i1 = 0; i1 < h; i1++)
  25.             {
  26.                 for (int i2 = 0; i2 < l; i2++)
  27.                 {
  28.                     try
  29.                     {
  30.                         codes.Add(System.Drawing.ColorTranslator.ToHtml(img.GetPixel(i2, i1)));
  31.                     }
  32.                     catch (ArgumentOutOfRangeException aorex)
  33.                     {
  34.                         Console.WriteLine("{0} - {1} * {2}", aorex.Message, i1, i2);
  35.                     }
  36.                 }
  37.                 tw = AddLines(tw, codes);
  38.                 codes = new List<string>();
  39.             }
  40.             tw.Close();
  41.         }
  42.  
  43.         private static TextWriter AddLines(TextWriter tw, List<string> pixels)
  44.         {
  45.             tw.Write("<tr>");
  46.             foreach(string s in pixels)
  47.             {
  48.                 tw.Write("<th style=\"background-color:" + s +"\"/>");
  49.             }
  50.             tw.Write("</tr>");
  51.             return tw;
  52.         }
  53.  
  54.         private static TextWriter CreatePage(string filename, string name)
  55.         {
  56.             string html = @"<html>
  57.                             <head>
  58.                                <style>      
  59.                                    .pixel th
  60.                                    {
  61.                                         width:1;
  62.                                         height:1;
  63.                                    }
  64.                                </style>
  65.                                 <title>" + name + @"</title>
  66.                             </head>
  67.                             <body>";
  68.             html += "<table border=\"0\"  cellpadding=\"0\" cellspacing=\"0\"  class=\"pixel\" >";
  69.             html += "<caption>" + name + "</caption>";
  70.  
  71.             TextWriter tw = new StreamWriter(filename);
  72.             tw.Write(html);
  73.             return tw;
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement