VBA Express Forum  




Go Back   VBA Express Forum > VBA Code & Other Help > Excel Help
     Feedback     
Register FAQ Members Arcade Knowledge Base Training Articles Consulting

Reply
 
Thread Tools Display Modes
Old 11-30-2006, 09:26 PM   #1
raykon

 
Joined: Nov 2006
Posts: 8
Kb Entries: 0
Articles: 0
Solved: Bitmaps with Excel VBA, how to create

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

Local Time: 03:36 AM
Local Date: 05-25-2013
Location:

 
Reply With Quote Top
Old 12-01-2006, 09:51 AM   #2
Ken Puls
 
Ken Puls's Avatar
Administrator

 
Joined: Aug 2004
Posts: 4,002
Kb Entries: 45
Articles: 0
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"


Ken Puls, CMA - Microsoft MVP (Excel)
I hate it when my computer does what I tell it to, and not what I want it to.

Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





Local Time: 03:36 AM
Local Date: 05-25-2013
Location:

 
Reply With Quote Top
Old 12-01-2006, 03:08 PM   #3
Erdin? E. Ka
 
Erdin? E. Ka's Avatar

 
Joined: Sep 2006
Posts: 264
Kb Entries: 0
Articles: 0
Hi Ken,

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

Thank you very much.


Erdin? E. Kara?am | Loves from Bursa city in Republic of T?rkiye

Local Time: 12:36 PM
Local Date: 05-25-2013
Location:

 
Reply With Quote Top
Old 12-01-2006, 06:02 PM   #4
johnske
 
johnske's Avatar
Administrator
Chat VP

 
Joined: Jul 2004
Posts: 2,872
Kb Entries: 67
Articles: 13
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...
VBA:
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
VBA tags courtesy of www.thecodenet.com

(edited)


You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

The major part of getting the right answer lies in asking the right question...


Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

Local Time: 08:36 PM
Local Date: 05-25-2013
Location:

 
Reply With Quote Top
Old 12-01-2006, 11:28 PM   #5
johnske
 
johnske's Avatar
Administrator
Chat VP

 
Joined: Jul 2004
Posts: 2,872
Kb Entries: 67
Articles: 13
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 to read about some practical uses for linked pictures.

HTH

(edited)


You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

The major part of getting the right answer lies in asking the right question...


Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

Local Time: 08:36 PM
Local Date: 05-25-2013
Location:

 
Reply With Quote Top
Old 12-02-2006, 04:21 AM   #6
xld
 
xld's Avatar
Distinguished Lord of VBAX

 
Joined: Apr 2005
Posts: 23,118
Kb Entries: 3
Articles: 2
Sound slike the guy wants Excel to be a paint type program.

Local Time: 11:36 AM
Local Date: 05-25-2013
Location:

 
Reply With Quote Top
Old 01-07-2012, 06:33 PM   #7
Zababcd

 
Joined: Jan 2012
Posts: 1
Kb Entries: 0
Articles: 0
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

Local Time: 09:36 PM
Local Date: 05-25-2013
Location:

 
Reply With Quote Top
Old 01-08-2012, 10:39 AM   #8
jorgea

 
Joined: Jan 2012
Posts: 5
Kb Entries: 0
Articles: 0
I'm glad to learn of an elaborate solution to this problem.

Local Time: 02:36 AM
Local Date: 05-25-2013
Location:

 
Reply With Quote Top
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -7. The time now is 03:36 AM.


Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Copyright © 2004 - 2012 VBA Express