Created
November 26, 2015 07:15
-
-
Save dck-jp/75bf633dc15af8f88f24 to your computer and use it in GitHub Desktop.
PointDrawing on Excel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "System.Drawing" | |
#r "Microsoft.VisualBasic" | |
using System; | |
using System.IO; | |
using System.Drawing; | |
using System.Linq; | |
using Microsoft.VisualBasic; | |
var imgFile = @"hogehoge.jpg"; | |
var filenameImg = imgFile; | |
//Excel は外部パラメータとして与えられたMicrosoft.Office.Interop.Excel.Application | |
Excel.DisplayAlerts = false; | |
//シートを生成し、縦横の幅を1ピクセルに調整 | |
var sheet = Excel.ActiveWorkbook.ActiveSheet; | |
sheet.Name = "dots"; | |
sheet.Select(); | |
//枠線が鬱陶しい(しかも処理重い)ので消去 | |
Excel.Windows.Application.ActiveWindow.DisplayGridlines = false; | |
//0.08や0.75はピクセル値への換算に必要な定数 | |
Excel.Cells.ColumnWidth = 1 * 0.08 * 2; | |
Excel.Cells.RowHeight = 1 * 0.75 * 2; | |
//画像の全ピクセルを拾いながら背景色として代入 | |
using (var bmp = new Bitmap(filenameImg)) | |
{ | |
var points = from x in Enumerable.Range(0, bmp.Width) | |
from y in Enumerable.Range(0, bmp.Height) | |
select new Point(x, y); | |
var pointsSuffled = points.OrderBy(i => Guid.NewGuid()).ToList(); | |
pointsSuffled.ForEach(p => { | |
Excel.Cells[p.Y+1, p.X+1].Interior.Color = bmp.GetPixel(p.X, p.Y); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment