This simple code snippet takes an image and converts it into a CSS image by converting each pixel to an HTML span element.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Web;
using System.Web.UI;
namespace ImageToCSS
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Bitmap img = (Bitmap)Image.FromFile(Server.MapPath("Penguins.jpg"));
int width = img.Width;
int height = img.Height;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
Color pixelColor = img.GetPixel(x, y);
Response.Write(string.Format("<span style='width:1px; height:1px; display:inline-block; background-color: rgb({0},{1},{2});'></span>", pixelColor.R, pixelColor.G, pixelColor.B));
}
Response.Write( "<br/>");
}
}
}
}
No comments:
Post a Comment