home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH4 / SRC / RESIZEF.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-01-03  |  21.5 KB  |  690 lines

  1. VERSION 4.00
  2. Begin VB.Form ResizeForm 
  3.    Caption         =   "Resize"
  4.    ClientHeight    =   5430
  5.    ClientLeft      =   1275
  6.    ClientTop       =   1065
  7.    ClientWidth     =   6885
  8.    Height          =   6120
  9.    Left            =   1215
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   362
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   459
  14.    Top             =   435
  15.    Width           =   7005
  16.    Begin VB.HScrollBar HBar 
  17.       Enabled         =   0   'False
  18.       Height          =   255
  19.       Index           =   1
  20.       Left            =   3480
  21.       SmallChange     =   10
  22.       TabIndex        =   9
  23.       Top             =   5160
  24.       Width           =   3135
  25.    End
  26.    Begin VB.VScrollBar VBar 
  27.       Enabled         =   0   'False
  28.       Height          =   4455
  29.       Index           =   1
  30.       Left            =   6600
  31.       SmallChange     =   10
  32.       TabIndex        =   8
  33.       Top             =   720
  34.       Width           =   255
  35.    End
  36.    Begin VB.PictureBox Swin 
  37.       Height          =   4455
  38.       Index           =   1
  39.       Left            =   3480
  40.       ScaleHeight     =   293
  41.       ScaleMode       =   3  'Pixel
  42.       ScaleWidth      =   205
  43.       TabIndex        =   7
  44.       Top             =   720
  45.       Width           =   3135
  46.       Begin VB.PictureBox Pict 
  47.          AutoRedraw      =   -1  'True
  48.          BorderStyle     =   0  'None
  49.          Height          =   1950
  50.          Index           =   1
  51.          Left            =   0
  52.          Picture         =   "RESIZEF.frx":0000
  53.          ScaleHeight     =   130
  54.          ScaleMode       =   3  'Pixel
  55.          ScaleWidth      =   154
  56.          TabIndex        =   10
  57.          Top             =   0
  58.          Width           =   2310
  59.       End
  60.    End
  61.    Begin VB.HScrollBar HBar 
  62.       Enabled         =   0   'False
  63.       Height          =   255
  64.       Index           =   0
  65.       Left            =   0
  66.       SmallChange     =   10
  67.       TabIndex        =   6
  68.       Top             =   5160
  69.       Width           =   3135
  70.    End
  71.    Begin VB.VScrollBar VBar 
  72.       Enabled         =   0   'False
  73.       Height          =   4455
  74.       Index           =   0
  75.       Left            =   3120
  76.       SmallChange     =   10
  77.       TabIndex        =   5
  78.       Top             =   720
  79.       Width           =   255
  80.    End
  81.    Begin VB.PictureBox Swin 
  82.       Height          =   4455
  83.       Index           =   0
  84.       Left            =   0
  85.       ScaleHeight     =   293
  86.       ScaleMode       =   3  'Pixel
  87.       ScaleWidth      =   205
  88.       TabIndex        =   3
  89.       Top             =   720
  90.       Width           =   3135
  91.       Begin VB.PictureBox Pict 
  92.          AutoRedraw      =   -1  'True
  93.          AutoSize        =   -1  'True
  94.          BorderStyle     =   0  'None
  95.          Height          =   15
  96.          Index           =   0
  97.          Left            =   0
  98.          Picture         =   "RESIZEF.frx":0446
  99.          ScaleHeight     =   1
  100.          ScaleMode       =   3  'Pixel
  101.          ScaleWidth      =   1
  102.          TabIndex        =   4
  103.          Top             =   0
  104.          Width           =   15
  105.       End
  106.    End
  107.    Begin VB.CommandButton CmdGo 
  108.       Caption         =   "Go"
  109.       Default         =   -1  'True
  110.       Enabled         =   0   'False
  111.       Height          =   375
  112.       Left            =   1200
  113.       TabIndex        =   2
  114.       Top             =   0
  115.       Width           =   615
  116.    End
  117.    Begin VB.TextBox ScaleText 
  118.       Height          =   285
  119.       Left            =   480
  120.       TabIndex        =   1
  121.       Text            =   "1.0"
  122.       Top             =   0
  123.       Width           =   615
  124.    End
  125.    Begin VB.Label SizeLabel 
  126.       Alignment       =   2  'Center
  127.       Height          =   255
  128.       Index           =   1
  129.       Left            =   3480
  130.       TabIndex        =   12
  131.       Top             =   420
  132.       Width           =   3135
  133.    End
  134.    Begin VB.Label SizeLabel 
  135.       Alignment       =   2  'Center
  136.       Height          =   255
  137.       Index           =   0
  138.       Left            =   0
  139.       TabIndex        =   11
  140.       Top             =   420
  141.       Width           =   3135
  142.    End
  143.    Begin VB.Label Label2 
  144.       Caption         =   "Scale"
  145.       Height          =   255
  146.       Left            =   0
  147.       TabIndex        =   0
  148.       Top             =   0
  149.       Width           =   495
  150.    End
  151.    Begin MSComDlg.CommonDialog FileDialog 
  152.       Left            =   1920
  153.       Top             =   -120
  154.       _Version        =   65536
  155.       _ExtentX        =   847
  156.       _ExtentY        =   847
  157.       _StockProps     =   0
  158.       CancelError     =   -1  'True
  159.    End
  160.    Begin VB.Menu mnuFile 
  161.       Caption         =   "&File"
  162.       Begin VB.Menu mnuFileLoad 
  163.          Caption         =   "&Load..."
  164.          Shortcut        =   ^L
  165.       End
  166.       Begin VB.Menu mnuFileSaveAs 
  167.          Caption         =   "Save As..."
  168.          Shortcut        =   ^A
  169.       End
  170.       Begin VB.Menu mnuFileSep 
  171.          Caption         =   "-"
  172.       End
  173.       Begin VB.Menu mnuFileExit 
  174.          Caption         =   "E&xit"
  175.       End
  176.    End
  177. Attribute VB_Name = "ResizeForm"
  178. Attribute VB_Creatable = False
  179. Attribute VB_Exposed = False
  180. Option Explicit
  181. Dim SysPalSize As Integer
  182. Dim NumStaticColors As Integer
  183. Dim StaticColor1 As Integer
  184. Dim StaticColor2 As Integer
  185. Dim LogPal As Integer
  186. Dim palentry(0 To 255) As PALETTEENTRY
  187. Dim wid As Long
  188. Dim hgt As Long
  189. Dim bytes() As Byte
  190. Dim ScaleFactor As Single
  191. ' ************************************************
  192. ' Draw the resized image at the proper scale.
  193. ' ************************************************
  194. Sub DrawImages()
  195. Dim wid As Single
  196. Dim hgt As Single
  197.     WaitStart
  198.     ScaleFactor = CSng(ScaleText.Text)
  199.     ' Resize using ShrinkPicture or EnlargePicture.
  200.     wid = Pict(0).ScaleWidth * ScaleFactor
  201.     hgt = Pict(0).ScaleHeight * ScaleFactor
  202.     Pict(1).Width = wid
  203.     Pict(1).Height = hgt
  204.     Pict(1).Cls
  205.     SizeLabel(1).Caption = _
  206.         Format$(Pict(1).ScaleWidth) & " x " & _
  207.         Format$(Pict(1).ScaleHeight)
  208.     If ScaleFactor > 1 Then
  209.         EnlargePicture Pict(0), Pict(1), _
  210.             0, 0, Pict(0).ScaleWidth - 2, Pict(0).ScaleHeight - 2, _
  211.             0, 0, wid - 1, hgt - 1
  212.     Else
  213.         ShrinkPicture Pict(0), Pict(1), _
  214.             0, 0, Pict(0).ScaleWidth - 1, Pict(0).ScaleHeight - 1, _
  215.             0, 0, wid - 1, hgt - 1
  216.     End If
  217.     DoEvents
  218.     ' Let each image repair its palette if needed.
  219.     Pict(0).ZOrder
  220.     DoEvents
  221.     Pict(1).ZOrder
  222.     DoEvents
  223.     HBar(1).Value = 0
  224.     VBar(1).Value = 0
  225.     wid = Pict(1).Width - Swin(1).ScaleWidth
  226.     If wid > 0 Then
  227.         HBar(1).Max = wid
  228.         HBar(1).Enabled = True
  229.     Else
  230.         HBar(1).Enabled = False
  231.     End If
  232.     hgt = Pict(1).Height - Swin(1).ScaleHeight
  233.     If hgt > 0 Then
  234.         VBar(1).Max = hgt
  235.         VBar(1).Enabled = True
  236.     Else
  237.         VBar(1).Enabled = False
  238.     End If
  239.     WaitEnd
  240. End Sub
  241. ' ************************************************
  242. ' Enlarge the picture in from_pic and place it
  243. ' in to_pic.
  244. ' ************************************************
  245. Sub EnlargePicture( _
  246.     ByVal from_pic As Control, ByVal to_pic As Control, _
  247.     ByVal fx1 As Integer, ByVal fy1 As Integer, _
  248.     ByVal fx2 As Integer, ByVal fy2 As Integer, _
  249.     ByVal tx1 As Integer, ByVal ty1 As Integer, _
  250.     ByVal tx2 As Integer, ByVal ty2 As Integer)
  251. Dim bm As BITMAP
  252. Dim hbm As Integer
  253. Dim status As Long
  254. Dim from_bytes() As Byte
  255. Dim to_bytes() As Byte
  256. Dim from_wid As Long
  257. Dim from_hgt As Long
  258. Dim to_wid As Long
  259. Dim to_hgt As Long
  260. Dim xscale As Single
  261. Dim yscale As Single
  262. Dim tx As Integer
  263. Dim ty As Integer
  264. Dim fx As Single
  265. Dim fy As Single
  266. Dim ifx As Single
  267. Dim ify As Single
  268. Dim dx As Single
  269. Dim dy As Single
  270. Dim c1 As Integer
  271. Dim c2 As Integer
  272. Dim c3 As Integer
  273. Dim c4 As Integer
  274. Dim i1 As Integer
  275. Dim i2 As Integer
  276. Dim clr As Integer
  277.     ' Compute the scaling parameters.
  278.     xscale = (tx2 - tx1) / (fx2 - fx1)
  279.     yscale = (ty2 - ty1) / (fy2 - fy1)
  280.     ' Get from_pic's pixels.
  281.     hbm = from_pic.Image
  282.     status = GetObject(hbm, BITMAP_SIZE, bm)
  283.     from_wid = bm.bmWidthBytes
  284.     from_hgt = bm.bmHeight
  285.     ReDim from_bytes(0 To from_wid - 1, 0 To from_hgt - 1)
  286.     status = GetBitmapBits(hbm, from_wid * from_hgt, from_bytes(0, 0))
  287.     ' Get to_pic's pixels.
  288.     hbm = to_pic.Image
  289.     status = GetObject(hbm, BITMAP_SIZE, bm)
  290.     to_wid = bm.bmWidthBytes
  291.     to_hgt = bm.bmHeight
  292.     ReDim to_bytes(0 To to_wid - 1, 0 To to_hgt - 1)
  293.     status = GetBitmapBits(hbm, to_wid * to_hgt, to_bytes(0, 0))
  294.         
  295.     ' Perform the enlargement.
  296.     For ty = ty1 To ty2
  297.         fy = (ty - ty1) / yscale + fy1
  298.         ify = Int(fy)
  299.         dy = fy - ify
  300.         For tx = tx1 To tx2
  301.             fx = (tx - tx1) / xscale + fx1
  302.             ifx = Int(fx)
  303.             dx = fx - ifx
  304.             ' Interpolate using the four nearest
  305.             ' pixels in from_pic.
  306.             c1 = palentry(from_bytes(ifx, ify)).peRed
  307.             c2 = palentry(from_bytes(ifx + 1, ify)).peRed
  308.             c3 = palentry(from_bytes(ifx, ify + 1)).peRed
  309.             c4 = palentry(from_bytes(ifx + 1, ify + 1)).peRed
  310.             ' Interpolate in the Y direction.
  311.             i1 = c1 * (1 - dy) + c3 * dy
  312.             i2 = c2 * (1 - dy) + c4 * dy
  313.             ' Interpolate the results in the X direction.
  314.             clr = i1 * (1 - dx) + i2 * dx
  315.             to_bytes(tx, ty) = NearestNonstaticGray(clr)
  316.         Next tx
  317.     Next ty
  318.     ' Update from_pic.
  319.     status = SetBitmapBits(hbm, to_wid * to_hgt, to_bytes(0, 0))
  320.     to_pic.Refresh
  321. End Sub
  322. ' ************************************************
  323. ' Shrink the picture in from_pic and place it
  324. ' in to_pic.
  325. ' ************************************************
  326. Sub ShrinkPicture( _
  327.     ByVal from_pic As Control, ByVal to_pic As Control, _
  328.     ByVal fx1 As Integer, ByVal fy1 As Integer, _
  329.     ByVal fx2 As Integer, ByVal fy2 As Integer, _
  330.     ByVal tx1 As Integer, ByVal ty1 As Integer, _
  331.     ByVal tx2 As Integer, ByVal ty2 As Integer)
  332. Dim bm As BITMAP
  333. Dim hbm As Integer
  334. Dim status As Long
  335. Dim from_bytes() As Byte
  336. Dim to_bytes() As Byte
  337. Dim from_wid As Long
  338. Dim from_hgt As Long
  339. Dim to_wid As Long
  340. Dim to_hgt As Long
  341. Dim xscale As Single
  342. Dim yscale As Single
  343. Dim tx As Integer
  344. Dim ty As Integer
  345. Dim x1 As Integer
  346. Dim y1 As Integer
  347. Dim x2 As Integer
  348. Dim y2 As Integer
  349. Dim X As Integer
  350. Dim Y As Integer
  351. Dim clr As Integer
  352.     ' Compute the scaling parameters.
  353.     xscale = (tx2 - tx1) / (fx2 - fx1)
  354.     yscale = (ty2 - ty1) / (fy2 - fy1)
  355.     ' Get from_pic's pixels.
  356.     hbm = from_pic.Image
  357.     status = GetObject(hbm, BITMAP_SIZE, bm)
  358.     from_wid = bm.bmWidthBytes
  359.     from_hgt = bm.bmHeight
  360.     ReDim from_bytes(0 To from_wid - 1, 0 To from_hgt - 1)
  361.     status = GetBitmapBits(hbm, from_wid * from_hgt, from_bytes(0, 0))
  362.     ' Get to_pic's pixels.
  363.     hbm = to_pic.Image
  364.     status = GetObject(hbm, BITMAP_SIZE, bm)
  365.     to_wid = bm.bmWidthBytes
  366.     to_hgt = bm.bmHeight
  367.     ReDim to_bytes(0 To to_wid - 1, 0 To to_hgt - 1)
  368.     status = GetBitmapBits(hbm, to_wid * to_hgt, to_bytes(0, 0))
  369.         
  370.     ' Skrink the image.
  371.     For ty = ty1 To ty2 - 1
  372.         y1 = Int((ty - ty1) / yscale + fy1)
  373.         y2 = Int((ty + 1 - ty1) / yscale + fy1) - 1
  374.         For tx = tx1 To tx2 - 1
  375.             x1 = Int((tx - tx1) / xscale + fx1)
  376.             x2 = Int((tx + 1 - tx1) / xscale + fx1) - 1
  377.             ' Average the values within the
  378.             ' from_pic box (x1, y1) - (x2, y2).
  379.             clr = 0
  380.             For Y = y1 To y2
  381.                 For X = x1 To x2
  382.                     clr = clr + palentry(from_bytes(X, Y)).peRed
  383.                 Next X
  384.             Next Y
  385.             clr = clr / (x2 - x1 + 1) / (y2 - y1 + 1)
  386.             to_bytes(tx, ty) = NearestNonstaticGray(clr)
  387.         Next tx
  388.     Next ty
  389.     ' Update from_pic.
  390.     status = SetBitmapBits(hbm, to_wid * to_hgt, to_bytes(0, 0))
  391.     to_pic.Refresh
  392. End Sub
  393. ' ***********************************************
  394. ' Load the control's palette so the non-static
  395. ' colors are grays. Map the logical palette to
  396. ' match the system palette. Convert the image to
  397. ' use the non-static grays.
  398. ' Set the following module global variables.
  399. '   LogPal      Image logical palette handle.
  400. '   palentry()  Image logical palette entries.
  401. '   wid         Width of image.
  402. '   hgt         Height of image.
  403. '   bytes(1 To wid, 1 To hgt)
  404. '               Image pixel values.
  405. ' ***********************************************
  406. Sub MatchGrayPalette(pic As Control)
  407. Dim sys(0 To 255) As PALETTEENTRY
  408. Dim i As Integer
  409. Dim bm As BITMAP
  410. Dim hbm As Integer
  411. Dim status As Long
  412. Dim X As Integer
  413. Dim Y As Integer
  414. Dim gray As Single
  415. Dim dgray As Single
  416. Dim c As Integer
  417. Dim clr As Integer
  418.     ' Make sure pic has the foreground palette.
  419.     pic.ZOrder
  420.     i = RealizePalette(pic.hdc)
  421.     DoEvents
  422.     ' Get the system palette entries.
  423.     i = GetSystemPaletteEntries(pic.hdc, 0, SysPalSize, sys(0))
  424.         
  425.     ' Get the image pixels.
  426.     hbm = pic.Image
  427.     status = GetObject(hbm, BITMAP_SIZE, bm)
  428.     wid = bm.bmWidthBytes
  429.     hgt = bm.bmHeight
  430.     ReDim bytes(1 To wid, 1 To hgt)
  431.     status = GetBitmapBits(hbm, wid * hgt, bytes(1, 1))
  432.     ' Make the logical palette as big as possible.
  433.     LogPal = pic.Picture.hPal
  434.     If ResizePalette(LogPal, SysPalSize) = 0 Then
  435.         Beep
  436.         MsgBox "Error resizing logical palette.", _
  437.             vbExclamation
  438.         Exit Sub
  439.     End If
  440.     ' Blank the non-static colors.
  441.     For i = 0 To StaticColor1
  442.         palentry(i) = sys(i)
  443.     Next i
  444.     For i = StaticColor1 + 1 To StaticColor2 - 1
  445.         With palentry(i)
  446.             .peRed = 0
  447.             .peGreen = 0
  448.             .peBlue = 0
  449.             .peFlags = PC_NOCOLLAPSE
  450.         End With
  451.     Next i
  452.     For i = StaticColor2 To 255
  453.         palentry(i) = sys(i)
  454.     Next i
  455.     i = SetPaletteEntries(LogPal, 0, SysPalSize, palentry(0))
  456.     ' Insert the non-static grays.
  457.     gray = 0
  458.     dgray = 255 / (StaticColor2 - StaticColor1 - 2)
  459.     For i = StaticColor1 + 1 To StaticColor2 - 1
  460.         c = gray
  461.         gray = gray + dgray
  462.         With palentry(i)
  463.             .peRed = c
  464.             .peGreen = c
  465.             .peBlue = c
  466.         End With
  467.     Next i
  468.     i = SetPaletteEntries(LogPal, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1))
  469.     ' Recreate the image using the new colors.
  470.     For Y = 1 To hgt
  471.         For X = 1 To wid
  472.             clr = bytes(X, Y)
  473.             With sys(clr)
  474.                 c = (CInt(.peRed) + .peGreen + .peBlue) / 3
  475.             End With
  476.             bytes(X, Y) = NearestNonstaticGray(c)
  477.         Next X
  478.     Next Y
  479.     status = SetBitmapBits(hbm, wid * hgt, bytes(1, 1))
  480.     ' Realize the gray palette.
  481.     i = RealizePalette(pic.hdc)
  482.     pic.Refresh
  483. End Sub
  484. ' ************************************************
  485. ' Return the index of the nonstatic gray closest
  486. ' to the given value (assuming the non-static
  487. ' colors are a gray scale created by
  488. ' MatchGrayPalette).
  489. ' ************************************************
  490. Function NearestNonstaticGray(c As Integer) As Integer
  491. Dim dgray As Single
  492.     If c < 0 Then
  493.         c = 0
  494.     ElseIf c > 255 Then
  495.         c = 255
  496.     End If
  497.     dgray = 255 / (StaticColor2 - StaticColor1 - 2)
  498.     NearestNonstaticGray = c / dgray + StaticColor1 + 1
  499. End Function
  500. Private Sub CmdGo_Click()
  501.     DrawImages
  502. End Sub
  503. Private Sub Form_Load()
  504.     ' Make sure the screen supports palettes.
  505.     If Not GetDeviceCaps(hdc, RASTERCAPS) And RC_PALETTE Then
  506.         Beep
  507.         MsgBox "This monitor does not support palettes.", _
  508.             vbCritical
  509.         End
  510.     End If
  511.     ' Get system palette size and # static colors.
  512.     SysPalSize = GetDeviceCaps(hdc, SIZEPALETTE)
  513.     NumStaticColors = GetDeviceCaps(hdc, NUMRESERVED)
  514.     StaticColor1 = NumStaticColors \ 2 - 1
  515.     StaticColor2 = SysPalSize - NumStaticColors \ 2
  516.     ' Make the pictures all use gray palettes.
  517.     ScaleFactor = 1
  518.     Me.Show
  519.     WaitStart
  520.     MatchGrayPalette Pict(0)
  521.     MatchGrayPalette Pict(1)
  522.     DoEvents
  523.     ' Let each image repair its palette if needed.
  524.     Pict(0).ZOrder
  525.     DoEvents
  526.     Pict(1).ZOrder
  527.     DoEvents
  528.     WaitEnd
  529. End Sub
  530. ' ***********************************************
  531. ' Reset the cursors for the form and all the
  532. ' picture boxes.
  533. ' ***********************************************
  534. Sub WaitEnd()
  535.     MousePointer = vbDefault
  536. End Sub
  537. ' ***********************************************
  538. ' Give the form and all the picture boxes an
  539. ' hourglass cursor.
  540. ' ***********************************************
  541. Sub WaitStart()
  542.     MousePointer = vbHourglass
  543.     DoEvents
  544. End Sub
  545. Private Sub Form_Resize()
  546. Const GAP = 5
  547. Dim wid As Single
  548. Dim status As Long
  549.     If WindowState = 1 Then Exit Sub
  550.     wid = (ScaleWidth - 2 * VBar(0).Width - 2 - GAP) / 2
  551.     hgt = ScaleHeight - HBar(0).Height - Swin(0).Top - 1
  552.     SizeLabel(0).Move 0, SizeLabel(0).Top, wid
  553.     Swin(0).Move 0, Swin(0).Top, wid, hgt
  554.     HBar(0).Move 0, Swin(0).Top + Swin(0).Height + 1, _
  555.         wid
  556.     VBar(0).Move Swin(0).Left + Swin(0).Width + 1, _
  557.         Swin(0).Top, VBar(0).Width, hgt
  558.     HBar(0).LargeChange = Swin(0).ScaleWidth
  559.     VBar(0).LargeChange = Swin(0).ScaleHeight
  560.     SizeLabel(1).Move VBar(0).Left + VBar(0).Width + GAP, _
  561.         SizeLabel(1).Top, wid
  562.     Swin(1).Move SizeLabel(1).Left, _
  563.         Swin(1).Top, wid, hgt
  564.     HBar(1).Move Swin(1).Left, Swin(1).Top + Swin(1).Height + 1, _
  565.         wid
  566.     VBar(1).Move Swin(1).Left + Swin(1).Width + 1, _
  567.         Swin(1).Top, VBar(1).Width, hgt
  568.     HBar(1).LargeChange = Swin(1).ScaleWidth
  569.     VBar(1).LargeChange = Swin(1).ScaleHeight
  570.     If HBar(0).Enabled Then
  571.         HBar(0).Max = Pict(0).Width - Swin(0).ScaleWidth
  572.         VBar(0).Max = Pict(0).Height - Swin(0).ScaleHeight
  573.     End If
  574.     If HBar(1).Enabled Then
  575.         HBar(1).Max = Pict(1).Width - Swin(1).ScaleWidth
  576.         VBar(1).Max = Pict(1).Height - Swin(1).ScaleHeight
  577.     End If
  578. End Sub
  579. Private Sub Form_Unload(Cancel As Integer)
  580.     End
  581. End Sub
  582. Private Sub HBar_Change(Index As Integer)
  583.     Pict(Index).Left = -HBar(Index).Value
  584. End Sub
  585. Private Sub HBar_Scroll(Index As Integer)
  586.     HBar_Change Index
  587. End Sub
  588. Private Sub mnuFileExit_Click()
  589.     Unload Me
  590. End Sub
  591. ' ***********************************************
  592. ' Load a new image file.
  593. ' ***********************************************
  594. Private Sub mnuFileLoad_Click()
  595. Dim fname As String
  596.     ' Allow the user to pick a file.
  597.     On Error Resume Next
  598.     FileDialog.filename = "*.BMP;*.ICO;*.RLE;*.WMF;*.DIB"
  599.     FileDialog.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  600.     FileDialog.ShowOpen
  601.     If Err.Number = cdlCancel Then
  602.         Exit Sub
  603.     ElseIf Err.Number <> 0 Then
  604.         Beep
  605.         MsgBox "Error selecting file.", , vbExclamation
  606.         Exit Sub
  607.     End If
  608.     On Error GoTo 0
  609.     fname = Trim$(FileDialog.filename)
  610.     FileDialog.InitDir = Left$(fname, Len(fname) _
  611.         - Len(FileDialog.FileTitle) - 1)
  612.     ' Load the picture.
  613.     WaitStart
  614.     LoadFromPict fname
  615.     WaitEnd
  616. End Sub
  617. ' ***********************************************
  618. ' Load the indicated file and prepare to work
  619. ' with its palette.
  620. ' ***********************************************
  621. Sub LoadFromPict(fname As String)
  622. Dim status As Long
  623. Dim wid As Integer
  624. Dim hgt As Integer
  625.     On Error GoTo LoadFileError
  626.     Pict(0).Picture = LoadPicture(fname)
  627.     On Error GoTo 0
  628.         
  629.     MatchGrayPalette Pict(0)
  630.     CmdGo.Enabled = True
  631.     HBar(0).Value = 0
  632.     VBar(0).Value = 0
  633.     HBar(0).Max = Pict(0).Width - Swin(0).ScaleWidth
  634.     VBar(0).Max = Pict(0).Height - Swin(0).ScaleHeight
  635.     wid = Pict(0).Width - Swin(0).ScaleWidth
  636.     If wid > 0 Then
  637.         HBar(0).Max = wid
  638.         HBar(0).Enabled = True
  639.     Else
  640.         HBar(0).Enabled = False
  641.     End If
  642.     hgt = Pict(0).Height - Swin(0).ScaleHeight
  643.     If hgt > 0 Then
  644.         VBar(0).Max = hgt
  645.         VBar(0).Enabled = True
  646.     Else
  647.         VBar(0).Enabled = False
  648.     End If
  649.     SizeLabel(0).Caption = Format$(Pict(0).ScaleWidth) & " x " & Format$(Pict(0).ScaleHeight)
  650.     Caption = "Resize [" & fname & "]"
  651.     Exit Sub
  652. LoadFileError:
  653.     Beep
  654.     MsgBox "Error loading file " & fname & "." & _
  655.         vbCrLf & Error$
  656.     Exit Sub
  657. End Sub
  658. ' ************************************************
  659. ' Allow the user to save the resized bitmap.
  660. ' ************************************************
  661. Private Sub mnuFileSaveAs_Click()
  662. Dim fname As String
  663.     ' Allow the user to pick a file.
  664.     On Error Resume Next
  665.     FileDialog.filename = "*.BMP;*.ICO;*.RLE;*.WMF;*.DIB"
  666.     FileDialog.Flags = cdlOFNOverwritePrompt + cdlOFNHideReadOnly
  667.     FileDialog.ShowSave
  668.     If Err.Number = cdlCancel Then
  669.         Exit Sub
  670.     ElseIf Err.Number <> 0 Then
  671.         Beep
  672.         MsgBox "Error selecting file.", , vbExclamation
  673.         Exit Sub
  674.     End If
  675.     On Error GoTo 0
  676.     fname = Trim$(FileDialog.filename)
  677.     FileDialog.InitDir = Left$(fname, Len(fname) _
  678.         - Len(FileDialog.FileTitle) - 1)
  679.     ' Save the picture.
  680.     WaitStart
  681.     SavePicture Pict(1).Image, fname
  682.     WaitEnd
  683. End Sub
  684. Private Sub VBar_Change(Index As Integer)
  685.     Pict(Index).Top = -VBar(Index).Value
  686. End Sub
  687. Private Sub VBar_Scroll(Index As Integer)
  688.     VBar_Change Index
  689. End Sub
  690.