Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2752
  • Last Modified:

VB6 Printer.Paintpicture doesn't work

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
The_Magician
Asked:
The_Magician
  • 20
  • 17
  • 2
  • +4
1 Solution
 
JohnMcCannCommented:
With Image1
Printer.PaintPicture .Picture, 1440, 1440, .Width, .Height, 0, 0, .Width, .Height
End With
0
 
The_MagicianAuthor Commented:
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
 
JohnMcCannCommented:
Try seeting Image1 AutoSize property to true.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
The_MagicianAuthor Commented:
I think you mean Stretch, and yes i've already set it to true.
0
 
JohnMcCannCommented:
Sorry I was thinking of the PictureBox.  I think the PictureBox would be better then you could set it's outosize to true.
0
 
The_MagicianAuthor Commented:
I think you mean Stretch, and yes i've already set it to true.
0
 
The_MagicianAuthor Commented:
Sorry to disappoint you again, but setting Autosize to true results in the picturebox resizing to fit the image loaded into it.
0
 
JohnMcCannCommented:
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
 
JohnMcCannCommented:
Sorry you will need to change these two liness

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

0
 
The_MagicianAuthor Commented:
With StretchBlt i get the error: sub or function not defined.
0
 
JohnMcCannCommented:
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
 
rspahitzCommented:
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
 
The_MagicianAuthor Commented:
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
 
The_MagicianAuthor Commented:
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
 
JohnMcCannCommented:
Sorry change ScrCopy  for vbSrcCopy.

I think it would be worth trying rspahitz suggestion.
0
 
The_MagicianAuthor Commented:
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
 
JohnMcCannCommented:
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
 
JohnMcCannCommented:
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
 
The_MagicianAuthor Commented:
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
 
JohnMcCannCommented:
Sorry it should have been vbTwips.
0
 
The_MagicianAuthor Commented:
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
 
JohnMcCannCommented:
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
 
The_MagicianAuthor Commented:
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
 
JohnMcCannCommented:
let's get one thing clear: i need to use both the printer.paintpicture as the Stretchblt? No one or the other
0
 
The_MagicianAuthor Commented:
when i only use Stretchblt it doesn't print any image.
0
 
JohnMcCannCommented:
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
 
The_MagicianAuthor Commented:
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
 
JohnMcCannCommented:
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
 
The_MagicianAuthor Commented:
0
 
JohnMcCannCommented:
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
 
JohnMcCannCommented:
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
 
The_MagicianAuthor Commented:
both of them don't print any image at all.
0
 
JohnMcCannCommented:
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
 
The_MagicianAuthor Commented:
this results in a chopped off square of the image.
0
 
JohnMcCannCommented:
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
 
The_MagicianAuthor Commented:
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
 
JohnMcCannCommented:
You are welcome.
0
 
cyberknetCommented:
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
 
JohnMcCannCommented:
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
 
cyberknetCommented:
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
 
FunkyMeisterCommented:
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
 
CleanupPingCommented:
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
 
SpideyModCommented:
PAQ'd and points refunded.

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 20
  • 17
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now