?
Solved

VB6 Printer.Paintpicture doesn't work

Posted on 2003-03-20
43
Medium Priority
?
2,682 Views
Last Modified: 2012-06-27
Hello,

I've got the following line of code:

Printer.PaintPicture Image1.Picture, 1440, 1440, 2655, 2535

This code resizes the picture perfectly if it's smaller then the sizes i've
set, but when the image is larger it doesn't resize and prints the picture
in it's full size. Does anyone know what i'm doing wrong?

Thnx,

Marvin van Dongen
0
Comment
Question by:The_Magician
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 20
  • 17
  • 2
  • +4
43 Comments
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8174819
With Image1
Printer.PaintPicture .Picture, 1440, 1440, .Width, .Height, 0, 0, .Width, .Height
End With
0
 

Author Comment

by:The_Magician
ID: 8174866
Thnx for your answer, but the code you suggested chops off a part of the image to make the size fit, but it doesn't resizes the picture.

Any ideas?
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8174987
Try seeting Image1 AutoSize property to true.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:The_Magician
ID: 8175011
I think you mean Stretch, and yes i've already set it to true.
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8175018
Sorry I was thinking of the PictureBox.  I think the PictureBox would be better then you could set it's outosize to true.
0
 

Author Comment

by:The_Magician
ID: 8175025
I think you mean Stretch, and yes i've already set it to true.
0
 

Author Comment

by:The_Magician
ID: 8175043
Sorry to disappoint you again, but setting Autosize to true results in the picturebox resizing to fit the image loaded into it.
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8175106
Ok,

Try this it uses a picturebox (Picture1) instead of the image control.

Dim nLeft As Integer
Dim nTop As Integer
Dim nWidth As Integer
Dim nHeight As Integer

   nLeft = 1440 / Printer.TwipsPerPixelX
   nTop = 1440 / Printer.TwipsPerPixelX
   nWidth = 2655 / Printer.TwipsPerPixelX
   nHeight = 2535 / Printer.TwipsPerPixelX

With Picture1
  .ScaleMode = vbPixels
  .AutoSize = True
  StretchBlt Printer.hdc, nLeft, nTop, _
               nWidth, nHeight, _
               .hdc, 0, 0, .ScaleWidth, .ScaleHeight, ScrCopy
End With
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8175120
Sorry you will need to change these two liness

nTop = 1440 / Printer.TwipsPerPixelY
nHeight = 2535 / Printer.TwipsPerPixelY

0
 

Author Comment

by:The_Magician
ID: 8175147
With StretchBlt i get the error: sub or function not defined.
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8175227
You will need to add this line to the top of the module

Private Declare Function StretchBlt Lib "gdi32" Alias "StretchBlt" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 8175560
There may be some competing issues here.

1) with the stretch property on, there may be problems stretching beyond the size of the windows vorders.  I've occassionally seen such problems, although it SHOULDN'T happen.

2) You don't need to use the stretch property at all since the paintpicture has paramters to handle that for you, which will avoid the problem of windows interfering with the resizing.

Try turning OFF the stretch property, then use JohnMcCann's original suggestion (or a variation):

Printer.PaintPicture Image1.Picture, 1440, 1440, 2655, 2535, 0, 0, Image1.width, Image1.height


0
 

Author Comment

by:The_Magician
ID: 8175593
thnx john i'm almost there. now i get the error that ScrCopy isn't defined. I tryed defining it, but it seems that is has to have a value.
0
 

Author Comment

by:The_Magician
ID: 8175615
rspahitz, your 2nd suggestion isn't working for two reasons:

1. I need the stretch property to be true because it displays an image to the user, and the size must be fixed.
2. even when i turn it off, it doesn't work.
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8175631
Sorry change ScrCopy  for vbSrcCopy.

I think it would be worth trying rspahitz suggestion.
0
 

Author Comment

by:The_Magician
ID: 8175681
John,

I'm getting further.......It resizes the picture a bit, bit it still keeps it's aspect ratio, and doesn't resize as much as I would want it. In other words...it's still to big.
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8175779
Try this

Dim nLeft As Long
Dim nTop As Long
Dim nWidth As Long
Dim nHeight As Long

  nLeft = 1440 / Printer.TwipsPerPixelX
  nTop = 1440 / Printer.TwipsPerPixelY
  nWidth = 2655 / Printer.TwipsPerPixelX
  nHeight = 2535 / Printer.TwipsPerPixelY

With Picture1
 .ScaleMode = vbPixels
 .AutoSize = True
 StretchBlt Printer.hdc, nLeft, nTop, _
              nWidth, nHeight, _
              .hdc, 0, 0, .ScaleWidth, .ScaleHeight, ScrCopy
End With

if it does not work try


Dim nLeft As Long
Dim nTop As Long
Dim nWidth As Long
Dim nHeight As Long

  nLeft = 1440 / Printer.TwipsPerPixelX
  nTop = 1440 / Printer.TwipsPerPixelY
  nWidth = 2655 / Screen.TwipsPerPixelX
  nHeight = 2535 / Screen.TwipsPerPixelY

With Picture1
 .ScaleMode = vbPixels
 .AutoSize = True
 StretchBlt Printer.hdc, nLeft, nTop, _
              nWidth, nHeight, _
              .hdc, 0, 0, .ScaleWidth, .ScaleHeight, ScrCopy
End With

Although I excpect the second will not work correct.

There is also

With Picture1
 .ScaleMode = vbPixels
 .AutoSize = True
 Printer.PaintPicture .Picture, 1440, 1440, 2655, 2535, 0, 0, .ScaleWidth, .ScaleHeight
End With




0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8175801
I try the later first

With Picture1
.ScaleMode = vbPixels
.AutoSize = True
Printer.PaintPicture .Picture, 1440, 1440, 2655, 2535, 0, 0, .ScaleWidth, .ScaleHeight
End With




0
 

Author Comment

by:The_Magician
ID: 8175849
The code doesn't work, because instead of printing the image it prints a black square where the image should be. When i use vbTwips as Scalemode it does work, but it still doesn't resize properly.
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8175983
Sorry it should have been vbTwips.
0
 

Author Comment

by:The_Magician
ID: 8176008
yeah i notticed that........but the problem remains. I'll give you what i got now to print it:

Dim nLeft As Integer
Dim nTop As Integer
Dim nWidth As Integer
Dim nHeight As Integer

  nLeft = 1440 / Printer.TwipsPerPixelX
  nTop = 1440 / Printer.TwipsPerPixelX
  nWidth = 2655 / Printer.TwipsPerPixelX
  nHeight = 2535 / Printer.TwipsPerPixelX

With Picture1
 .ScaleMode = vbPixels
 .AutoSize = True
 StretchBlt Printer.hdc, nLeft, nTop, _
              nWidth, nHeight, _
              .hdc, 0, 0, .ScaleWidth, .ScaleHeight, vbSrcCopy
End With
With Picture1
.ScaleMode = vbPixels
.AutoSize = True
Printer.PaintPicture .Picture, 1440, 1440, 2655, 2535, 0, 0, .ScaleWidth, .ScaleHeight
End With

Hope you can find what's going wrong.
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8176164
Are you using all of that code I assume not but if you are

This is one method and should be

With Picture1
  .ScaleMode = vbTwips
  .AutoSize = True
  Printer.PaintPicture .Picture, 1440, 1440, 2655, 2535, 0, 0, .Width, .Height
End With

The other approach would be

Dim nLeft As Integer
Dim nTop As Integer
Dim nWidth As Integer
Dim nHeight As Integer

 nLeft = Can you get the Left co-ordinate in pixels
 nTop = Can you get the top co-ordinate in pixels
 nWidth = Can you get the width in pixels
 nHeight = Can you get the Height in pixels

printer.scalemode=vbpixels
With Picture1
.ScaleMode = vbPixels
.AutoSize = True
StretchBlt Printer.hdc, nLeft, nTop, _
             nWidth, nHeight, _
             .hdc, 0, 0, .ScaleWidth, .ScaleHeight, vbSrcCopy
End With


Also these lines

nLeft = 1440 / Printer.TwipsPerPixelX

Are deviding the 1440 by something like 2.4 and returning a whole number which will have been rounded.  This could be where the accuracy is being lost which is why I suggested above to place the pixel measurements straight in.

When you say your image is not resizing properly.  Can you explain this in more detail.   I am intrested in the results of each apporach the size of the image in relation to the size you want, and its postion.

0
 

Author Comment

by:The_Magician
ID: 8176224
let's get one thing clear: i need to use both the printer.paintpicture as the Stretchblt?

And for the explaining: I'm creating a program that enables you to manage your recipes. I've got a layout for the print here: http://home.wanadoo.nl/marvinvdongen/Layout.GIF 

As you can see the image size has to be set, so there has to be some stretching. Now when the picture doesn't resize properly, it sets the left and bottom right, but it shifts the image up, so that the image is at "Description".

Hope this helps.
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8176265
let's get one thing clear: i need to use both the printer.paintpicture as the Stretchblt? No one or the other
0
 

Author Comment

by:The_Magician
ID: 8176283
when i only use Stretchblt it doesn't print any image.
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8176287
It should not be shifting the image if the co-ordinates are correct it will draw in the square for the picture.

Well stay with this one for a minute

Printer.ScaleMode = vbTwips
With Picture1
 .ScaleMode = vbTwips
 .AutoSize = True
 Printer.PaintPicture .Picture, 1440, 1440, 2655, 2535, 0, 0, .Width, .Height
End With

Try adjusting these values 1440, 1440, 2655, 2535.

0
 

Author Comment

by:The_Magician
ID: 8176356
It still goes wrong, and i'll show you how.

With the following code:
Printer.PaintPicture .Picture, 1440, 2800, 2655, 2535, 0, 0, 2655, 2535

I get this image:
http://home.wanadoo.nl/marvinvdongen/Wrong2.pdf

And with this code:
Printer.PaintPicture .Picture, 1440, 2800, 2655, 2535

i get this image:
http://home.wanadoo.nl/marvinvdongen/Wrong1.pdf

An example of how it should be:
http://home.wanadoo.nl/marvinvdongen/Good.pdf

BTW: Don't mind the image (just picked one that didn't worked) and the text (It's in Dutch)
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8176419
Can you post just gifs or jpegs

What about this

Printer.ScaleMode = vbTwips
With Picture1
.ScaleMode = vbTwips
.AutoSize = True
Printer.PaintPicture .Picture, 1440, 1440, 2655, 2535, 0, 0, .Width, .Height
End With
0
 

Author Comment

by:The_Magician
ID: 8176471
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8176599
And this one

Dim nLeft As Long
Dim nTop As Long
Dim nWidth As Long
Dim nHeight As Long

nLeft = 1440 / Screen.TwipsPerPixelX
nTop = 1440 / Screen.TwipsPerPixelY
nWidth = 2655 / Screen.TwipsPerPixelX
nHeight = 2535 / Screen.TwipsPerPixelY

Printer.ScaleMode = vbPixels

With Picture1
  .ScaleMode = vbPixels
  .AutoSize = True
  StretchBlt Printer.hdc, nLeft, nTop, _
            nWidth, nHeight, _
            .hdc, 0, 0, .ScaleWidth, .ScaleHeight, vbSrcCopy
End With

0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8176609
And this one

Dim nLeft As Long
Dim nTop As Long
Dim nWidth As Long
Dim nHeight As Long

nLeft = 1440 / Printer.TwipsPerPixelX
nTop = 1440 / Printer.TwipsPerPixelY
nWidth = 2655 / Printer.TwipsPerPixelX
nHeight = 2535 / Printer.TwipsPerPixelY

Printer.ScaleMode = vbPixels

With Picture1
 .ScaleMode = vbPixels
 .AutoSize = True
 StretchBlt Printer.hdc, nLeft, nTop, _
           nWidth, nHeight, _
           .hdc, 0, 0, .ScaleWidth, .ScaleHeight, vbSrcCopy
End With
0
 

Author Comment

by:The_Magician
ID: 8176813
both of them don't print any image at all.
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8178140
Ok well go back to the Image control.

set the stretch to true and its dimensions as required to print.

Printer.ScaleMode = vbTwips
With Image1
  Printer.PaintPicture .Picture, 1440, 1440, 2655, 2535, 0, 0, .Width, .Height
End With
0
 

Author Comment

by:The_Magician
ID: 8179176
this results in a chopped off square of the image.
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8180466
Take a look at this post

http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20558013.html


It seems to be the same problem you have.

Have a go at the solution suggested and let me know how you get on.

0
 

Author Comment

by:The_Magician
ID: 8180509
I've solved the problem myself, in a totally diferent way. I've added a picturebox, and added the following code:

picNew.Width = 2600
picNew.Height = 2600
picNew.AutoRedraw = True
picNew.PaintPicture Image1.Picture, 0, 0, picNew.ScaleWidth, picNew.ScaleHeight, 0, 0
Printer.PaintPicture picNew.Image, 1440, 1440 + 230 + 1400, 2600, 2600

This resizes the image and puts it in picNew. So that when i print the picure, it doesn't has to be resized. although you didn't gave the answer, i really want to thank you for your help and your time.

Greetz,

The Magician
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8181215
You are welcome.
0
 
LVL 1

Expert Comment

by:cyberknet
ID: 8181819
JohnMcCann:
Regarding
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20558013.html


My problem [the question referred to] is a little different. I can't use LoadPicture to load the image, so as far as vb is concerned, there is nothing in the .Picture object. Hence it will not let you me PaintPicture. StretchBlt and BitBlt will not work to copy the image from the form to the printer (different device types, windows will not allow a direct copy). GetDIBits and SetDIBits will copy it somehow, but I dont have the know-how to do it, and thus far, it looks like I do not have enough points to assign to make it worth someone's time to code it for me.
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8186300
I have had a little look around for you and found this

http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/VB_Databases/Q_10289836.html

This will defintly do what you require and some.  When I get chance later this weekend I will chop the code down to that parts you require.  
0
 
LVL 1

Expert Comment

by:cyberknet
ID: 8186546
Thanks John. Actually, someone just informed me that if you blit to a picture/form's DC, although the Picture property is not populated, the Image property is. And Image is also StdPicture object. So you can just do:

set object.Picture = object.Image

amazing, I never knew that!

That said, it seems like there is a problem with the image library I am working with when opening multipage TIFFs.

Yikes.

Thanks for your help though, I really appreciate it!
0
 
LVL 1

Expert Comment

by:FunkyMeister
ID: 8200137
Actually, the image you're getting originally was wrong in dimensions, is due to the fact that going from say:

800x600 to 1024x768 for a size change, is not aspect savvy.  Same goes for going to a printed page, the ratio can't be derived from changing the sizes and letting the Picture box resize (it won't do it via aspect ratio, but by the sizes you give it).  Think of resizing the image by doing a "Zoom", remember most calculations for Zooming a picture is done in percentages.
0
 

Expert Comment

by:CleanupPing
ID: 8531930
Hi The_Magician,
This old question (QID 20557568) needs to be finalized -- accept an answer, split points, or get a refund.  Please see http://www.cityofangels.com/Experts/Closing.htm for information and options.
0
 

Accepted Solution

by:
SpideyMod earned 0 total points
ID: 8536480
PAQ'd and points refunded.

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question