Note I was using Visual studio 2010 and Excel 2010 for this!
But I decided to try and colour a sheet from a JPG dont ask me why, but it seemed like a fun application, the first thing I found is that there is a large lack of information but here is the code incase you wish to try!
Imports System.Drawing Imports Microsoft.Office.Interop.Excel Public Class ThisWorkbook Private Sub ThisWorkbook_Startup() Handles Me.Startup Dim StepSize As Integer = 4 'speed up by only processing every nth pixel Dim MyImage As New Bitmap("C:\Users\Daz\Pictures\Elephants\DSCF0789.jpg") Dim PixelColor As Color ' Contains the pixel from the image Dim GreyScale As Integer ' used in converting to greyscale, since it overloads the maximum formatting otherwise Me.Application.ScreenUpdating = False Me.Application.EnableEvents = False For x = 0 To MyImage.Width - 1 Step StepSize Me.Sheets(1).Range("A1").offset(1, x / StepSize).columnwidth = 0.2 ' seemed to be 2 pixel wide excel cells Next For y = 0 To MyImage.Height - 1 Step StepSize Me.Sheets(1).Range("A1").offset(y / StepSize, 1).rowheight = 1.8 ' seemed to be 2 pixel wide excel cells Next For x = 0 To MyImage.Width - 1 Step StepSize For y = 0 To MyImage.Height - 1 Step StepSize 'read pixel from bitmap PixelColor = MyImage.GetPixel(x, y) ' convert pixel into greyscale GreyScale = PixelColor.R * 0.3 + PixelColor.G * 0.59 + PixelColor.B * 0.11 ' set cell on excel sheet to pixel color Me.Sheets(1).Range("A1").offset(y / StepSize, x / StepSize).interior.color = RGB(GreyScale, GreyScale, GreyScale) Next Next ' select image range Me.Sheets(1).range("A1:" & Me.Sheets(1).Range("A1").offset(MyImage.Height / StepSize - 1, MyImage.Width / StepSize - 1).address()).select() 'zoom to full image Me.Windows(1).Zoom = True Me.Application.EnableEvents = True Me.Application.ScreenUpdating = True MyImage = Nothing End Sub Private Sub ThisWorkbook_Shutdown() Handles Me.Shutdown End Sub End Class
The resulting file is here (its too big to post here) :-
http://cid-7c0eef3f7aa6f25b.skydrive...0Pricture.xlsx
ignore the error, as I havent included the compiled code!
Last edited by squiggler47; 05-13-2010 at 02:00 PM. Reason: Added Link
Regards
Darren
Update 12-Nov-2010 Still job hunting!
If you are happy with the results, please add to our reputation by clicking the blue scales icon in the blue bar of the post.
Learn something new each day, Embrace change do not fear it, evolve and do not become extinct!
Hi squiggler47,
I am unable to try this. I would like to try this code. I am using Excel 2007. Do I need to reference something for this ? Please guide me.
Regards,
Daksh
you need Visual Studio 2005 - 2010 with VSTO installed, (express editions wont work) and you select an excel 2007/2010 automation project!
Regards
Darren
Update 12-Nov-2010 Still job hunting!
If you are happy with the results, please add to our reputation by clicking the blue scales icon in the blue bar of the post.
Learn something new each day, Embrace change do not fear it, evolve and do not become extinct!
Daksh, please ask your question in one of the question forums. This isn't one.
Thanks.
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks