PDA

View Full Version : Solved: Bitmaps with Excel VBA, how to create



raykon
11-30-2006, 10:26 PM
Hi folks,

How does one create a bitmap file with Excel VBA?

All I need is the very basics, like how to create a file that is, for example, 100 pixels tall and 200 pixels wide, with a blue triangle and white background, or anything remotely like that.

Thanks, yet again, in advance.

- Ray

Ken Puls
12-01-2006, 10:51 AM
Do you need the code, or just the functionality? I haven't tried this myself, but you may want to look at this:

"Create Bitmap from Selection" (http://www.excelmaniacs.com/downloads.htm)

Erdin? E. Ka
12-01-2006, 04:08 PM
Hi Ken,

I tried it and my opinion is; this add-in is very useful.

Thank you very much.:hi:

johnske
12-01-2006, 07:02 PM
Also, Excel (well xl2000+ anyway) has a built-in camera function, here's some very old code I have that gives an example usage for you to play with...
Option Explicit
'
Sub DoCamera()
'
Dim CaptureRange As Range
Dim OutputRange As Range
'
'Prompt user for range to capture
On Error Resume Next
Set CaptureRange = Application.InputBox(Prompt:= _
"Select the range you would like to capture.", _
Title:="User Input Required", _
Default:=ActiveCell.Address, Type:=8)
If CaptureRange Is Nothing Then End
On Error GoTo 0
'
'Copy range to Clipboard as picture
CaptureRange.CopyPicture xlScreen, xlBitmap
'
'Prompt user for range to paste to
On Error Resume Next
Set OutputRange = Application.InputBox(Prompt:= _
"Select the range on which you would like to paste.", _
Title:="User Input Required", _
Default:=ActiveCell.Address, Type:=8)
If OutputRange Is Nothing Then End
'
'Paste picture to output range
OutputRange.PasteSpecial
Selection.Formula = CaptureRange.Address
ActiveSheet.Shapes(1).Select
With Selection.ShapeRange
.ScaleWidth 1.5, msoFalse, msoScaleFromTopLeft
.ScaleHeight 1.5, msoFalse, msoScaleFromTopLeft
'.Cut
End With
End Sub



(edited)

johnske
12-02-2006, 12:28 AM
Here's some more ideas you can play with...

The picture tool can be found in View > Toolbars > customize > tools (it has a picture of a camera on it) - drag it and put it on a toolbar somewhere.

Now (you can record all this if you like)
Select a range - the range can include shapes, pictures (whatever) and click the camera tool. Your mouse icon is now a cross, put the cross anywhere on the sheet (or another sheet {this can also be in another workbook}) left-click and drag the mouse icon. You'll now see a picture there, and in the formula bar you'll see the address of the selected range - this is because the picture is now linked to your original range. Any changes you now make (including any changes to shapes, cell formatting, etc) in the original range will show in this linked picture, and, if you change the range link in the formula bar, you'll get a new picture of this other range. This allows you create a "window" (or any number of them) that allows you to view changes on other worksheets when changes are made to the active sheet.

Another 'bonus' is that double-clicking the picture will activate the sheet and select the range it's linked to (and if the picture is linked to a closed book it will open that book)

To make an unlinked picture - select a range as before, hold down Shift, go to Edit and select Copy Picture - you'll get a dialog box offering a choice of Picture or Bitmap - press OK and let go of Shift. Select the range where you want your picture, hold down Shift, go to Edit again and select Paste Picture (n.b. those options are only available while holding down Shift).

Note that you can also format the picture if you like...

Have a read here (http://www.exceluser.com/solutions/traffic.htm) to read about some practical uses for linked pictures.

HTH :)

(edited)

Bob Phillips
12-02-2006, 05:21 AM
Sound slike the guy wants Excel to be a paint type program.

Zababcd
01-07-2012, 07:33 PM
I see that this is 'solved', but perhaps someone will be interested in a more versatile solution.
See my blog.
zababcdblog.blogspot.com/2012/01/how-to-write-bmp-file-from-excel-vba.html
And
zababcdblog.blogspot.com/2012/01/how-to-write-bmp-file-from-excel-vba-2.html

It's actually very simple to write it directly as bytes, once you know the bitmap file structure. The second link is a slight improvement on the first so that you don't need to read from a file.

The blue triangle on a white background (for example), would be written like this:
For x = 1 To ImgHeight
For y = 1 To ImgWidth
If y <= (ImgWidth + 1)/2 Then 'Left half of the picture,
'including middle column if any
If x >= 2*y/(ImgWidth + 1) Then 'If the row is below or at
'the percentage of height that the column is of halfway
ImageColours(x, y, 1) = 0 'no red
ImageColours(x, y, 2) = 0 'no green
ImageColours(x, y, 3) = 255 'all blue
Else
For z = 1 to 3
ImageColours(x, y, z) = 255 'all colours = white
Next z
End If
Else 'if it's on the right side of the image, then the left
For z = 1 to 3 'is already done so just reflect the image
ImageColours(x, y, z) = ImageColours(x, ImgWidth - y + 1, z)
Next z
End If
Next y
Next x

jorgea
01-08-2012, 11:39 AM
I'm glad to learn of an elaborate solution to this problem.