centre picture in picture box
centre picture in picture box
centre picture in picture box
centre picture in picture box
centre picture in picture box
centre picture in picture box centre picture in picture box centre picture in picture box centre picture in picture box centre picture in picture box centre picture in picture box centre picture in picture box centre picture in picture box
centre picture in picture box centre picture in picture box
centre picture in picture box
Go Back  Xtreme Visual Basic Talk > > > centre picture in picture box


Reply
 
Thread Tools Display Modes
  #1  
Old 01-17-2017, 12:05 PM
starmanMike starmanMike is offline
Junior Contributor
 
Join Date: Oct 2005
Location: near Norwich, UK
Posts: 287
Default centre picture in picture box


Hi,
And thanks to the useful replies to my previous question, but I have a sort of followup. I have found a routine that enables zooming in to the picture and it works. However, the picture is zoomed into the top left but I would like to have the centre of the picture zoomed in. I have tried this myself, assuming it would be something like this for the magnified picture:
Code:
picbox.picture.left = (some offset based on the size of the picbox)
picbox.picture.top = (similarly)
But it seems that .left and .top are not properties of 'picture' - so how can this be done?
Reply With Quote
  #2  
Old 01-17-2017, 04:19 PM
passel's Avatar
passelcentre picture in picture box passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,026
Default

What does the code look like?
How is the zooming being done, i.e. is the image being drawn with different size source and destination rectangles, or is the size of the picturebox being changed, etc...

The general procedure I usually use goes along the lines of knowing what the current scale factor is, what coordinate is currently located at the upper left corner, and what the coordinate is at the center of the image.

The approach is to keep the center coordinate the same, and change the top,left coordinate based on the current scale factor.
The actual code will depend on what method you are using to implement the zoomed image.

As an example, say that I am increasing the size of the picturebox, and the picturebox is set to automatically stretch the image when you resize the picturebox.

We start with a picturebox that is 100x100 pixels at 1.0 zoom level.
The upper left corner of the picturebox is at 50,50 so the center of the picturebox is at 100,100.
The difference in x and y from the center of the picturebox to the upperleft corner is (-50, -50) at 1X.

Now, if we scale the picture to 2X, then the picturebox will be twice the size (200,200) and the upper left corner will be 2 times further away from the center (-100, -100).
Since the center is at (100,100), the upperleft has moved from (50,50) to (0,0), i.e (100,100) + (-100, -100).

If we zoom the image to 3x, the upper left will be (100,100) + (-150,-150), which is (-50,-50).

So, the value of what the top left should be is easily calculated if you keep track of your original (dx,dy) value from the original center to the original left top.
You just multiply the original (dx,dy) value by your scale factor and add it to your original center (x,y) value to get the scaled left top.

If you're drawing, the principle is the same, just what values you are modifying in your drawing commands change. And there are different ways to do the drawing, e.g. draw the image larger with the offset like above, or better, keep the destination rectangle the same size (fill your drawing area), and scale the source rectangle smaller to zoom up a small part of the image to fill the larger area. Doing it that way is much more memory efficient and not as likely to take a long time to draw or fail because the zoom value has increased to the point you overflow the maximum size you can draw.

Without knowing what code you are using to zoom, we can't really say specifically what your code would look like.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #3  
Old 01-18-2017, 06:17 AM
starmanMike starmanMike is offline
Junior Contributor
 
Join Date: Oct 2005
Location: near Norwich, UK
Posts: 287
Default

Many thanks!
Although I don't actually 'get' the API stuff specifically, this is what I found and it works, so OK that far, but if I could find code that did the same thing more simply 'for me' I'd use it. But for now, that's OK. Also, the maths isn't a problem.
The picture box stays the same size, and as you say in the last para, I will be drawing and filling the drawing area (i.e., the picture box) by re-scaling the central area. Only modest rescaling is needed - I'd use about 1.5x.
Lots of thanks for your useful help.
Code:
Private Declare Function StretchBlt Lib "gdi32" (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
    Private Const SRCAND = &H8800C6  ' (DWORD) dest = source AND dest
    Private Const SRCCOPY = &HCC0020 ' (DWORD) dest = source
    Private Const SRCINVERT = &H660046       ' (DWORD) dest = source XOR dest
    Private Const SRCPAINT = &HEE0086        ' (DWORD) dest = source OR dest
     
    Function StretchPic(picBox As PictureBox, times As Integer) As Long
    StretchBlt picBox.hdc, 0, 0, picBox.ScaleWidth * times, picBox.ScaleHeight * times, picBox.hdc, 0, 0, picBox.ScaleWidth, picBox.ScaleHeight, SRCCOPY
    End Function

StretchPic PictureBox, 2 '2 for 2 times the size..

Last edited by passel; 01-18-2017 at 04:41 PM. Reason: Help forum presentation by wrapping long declaration
Reply With Quote
  #4  
Old 01-18-2017, 01:09 PM
Cerian Knight's Avatar
Cerian Knightcentre picture in picture box Cerian Knight is offline
Polymath (in disciplina)

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 4,193
Default

If the StrechBlt API is working for you, I would stay with it.

If you are interested, here is a discussion on some possible VB intrinsic alternative methods (that have their own quirks/limitations):
http://www.vbforums.com/showthread.p...Render-(How-To)
__________________
I got all the answers wrong on the GLAT, apparently even #9 (where I put a period in the middle of the box and labeled it 'singularity ripe for rapid inflation').
Reply With Quote
  #5  
Old 01-18-2017, 05:51 PM
OnErr0r's Avatar
OnErr0rcentre picture in picture box OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,774
Default

I posted an example of using Render in 2004: http://www.xtremevbtalk.com/381285-post4.html
__________________
Quis custodiet ipsos custodues.
Reply With Quote
  #6  
Old 01-18-2017, 08:31 PM
passel's Avatar
passelcentre picture in picture box passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,026
Default

Quote:
Originally Posted by starmanMike View Post
Many thanks!
Although I don't actually 'get' the API stuff specifically, this is what I found and it works, so OK that far,...
You say it works, but I don't see that it can work, at least not for very long.
First, you mentioned zooming 1.5x, but your "times" value is an Integer so can't be 1.5, i.e. your example used 2 for 2x, so that is why that worked.

But the code is also using the image and overwriting itself (rather than have a source image, and a destination image) so if you zoomed in and out a few times your image would be pretty much destroyed.

Here is a better example (it doesn't destroy itself) and doesn't require an extra source image. The picturebox already has a couple of areas it can store images ( .Picture and .Image) and the PaintPicture method of the picturebox can access the .Picture as the source, and will draw on the .Image as the destination (if .Autoredraw is set true).

An example of using that, off the top of my head, would be like this.
Just for the example, I added a picturebox to a form and set its picture to some image in the IDE.
I added a horizontal scrollbar and set its min value to 100, and its max value to 1000. I divide the value by 200 so you end up with zoom values in the range .5 to 5x.
I fill the picturebox with a black background first in case your zoom value is less than 1.0 so you don't see garbage around the shrunken image, you have a black border for non-image area.

The code doesn't do any proportional image scaling checks. If your picturebox's width and height match your picture's width and height proportions, then you won't need any compensation in the code.

This is just a simple example quickly written, so not production code.
Code:
Option Explicit

Sub Zoom(picBox As PictureBox, times As Single)
  Dim destWidth As Single, destHeight As Single
  Dim destTop As Single, destLeft As Single
  
  With picBox
    .AutoRedraw = True
    picBox.Line (0, 0)-(.ScaleWidth, .ScaleHeight), vbBlack, BF
    destWidth = picBox.ScaleWidth * times
    destHeight = picBox.ScaleHeight * times
    destTop = (picBox.ScaleHeight - destHeight) / 2
    destLeft = (picBox.ScaleWidth - destWidth) / 2
    picBox.PaintPicture picBox.Picture, destLeft, destTop, destWidth, destHeight
  End With
End Sub

Private Sub HScroll1_Scroll()
  Dim v As Single
  v = HScroll1.Value / 200
  Zoom Picture1, v
End Sub
p.s. I believe OnError's example using Render does allow for a better quality zoom, if I remember correctly. Don't do a lot of VB6 any more.

p.p.s. The example using the scrollbar only zooms the image when you drag on the thumb of the scrollbar. Need to add code to the Value Changed event if you want to also cover clicking on the scrollbar area or arrow buttons to change the value.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 01-18-2017 at 08:40 PM.
Reply With Quote
  #7  
Old 01-19-2017, 10:45 AM
starmanMike starmanMike is offline
Junior Contributor
 
Join Date: Oct 2005
Location: near Norwich, UK
Posts: 287
Default

Thanks a lot, I'll give that a try. One apology - I did change the 'times' variable in the function declaration to 'single' so that it would accept numbers like 1.3 etc. Sorry about not mentioning that.
Reply With Quote
  #8  
Old 01-20-2017, 05:54 AM
starmanMike starmanMike is offline
Junior Contributor
 
Join Date: Oct 2005
Location: near Norwich, UK
Posts: 287
Default

Well, I tried it, and all I can say is WOW!
You, sir, are a genius. Huge thanks - it works better than my existing 'zoom in' and 'zoom out' buttons (from previous working version whose source code was lost) because now you get fine tuning on the zoom. And I can now dispense with all the API stuff that I didn't understand. At least with your version I can get to understand it! So all round a great piece of help. Thanks so much.
Reply With Quote
Reply

Tags
picture, centre, zoomed, size, based, offset, picbox.picture.left, magnified, .left, properties, similarly, picbox.picture.top, .top, picbox, top, previous, question, sort, followup, replies, box, found, routine, left, assuming


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
centre picture in picture box
centre picture in picture box
centre picture in picture box centre picture in picture box
centre picture in picture box
centre picture in picture box
centre picture in picture box centre picture in picture box centre picture in picture box centre picture in picture box centre picture in picture box centre picture in picture box centre picture in picture box
centre picture in picture box
centre picture in picture box
 
centre picture in picture box
centre picture in picture box
 
-->