Solved

Help please - Splitting images into pieces

Posted on 2002-04-13
13
373 Views
Last Modified: 2010-05-02
Hi,

what I need is to split an image file(.bmp, .jpg, .gif, .tiff) into a predefined number of pieces(image files in the same file format). All pieces to have the same image size.
The ideal solution will be

function split_img(img_path, divide_X, divide_Y)
.....
End function

where img_path is the path to the image file to be splitted.
divide_X, divide_Y will define how many pieces to split the image into

If called like
split_img("C:\Test\img.gif", 3, 2)
the result should be 6 gif files named img1...img6.gif, each of them having an width of img.gif_width/3 and heigth of img.gif_heigth/2, saved in the same folder where the original file is.
If img.gif is let's say 1000x1000 pixels, the result should be 6 files 333x500 pixels.

Thanks
0
Comment
Question by:peterned
13 Comments
 

Author Comment

by:peterned
ID: 6939120
Looks like I can't assign more than 300 points to a question, but for a working answer to this(a piece of code that will do what I explained) I'll assign 600 by asking a fake question or something like that - I relly badly need a solution.
0
 
LVL 15

Expert Comment

by:ameba
ID: 6939422
img1.BMP to img6.BMP isn't OK, right?
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6939575
hearing...
0
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 300 total points
ID: 6939649
Function split_img(img_path As String, divide_X As Integer, divide_Y As Integer)
Dim pic As StdPicture
Dim PicH As Long, PicW As Long
Dim sectionH As Long, sectionW As Long
Dim l As Integer, a As Integer, iCount As Integer
Dim filename As String, strRet

filename = Mid$(img_path, InStrRev(img_path, "\", , vbTextCompare) + 1)
strRet = Split(filename, ".", , vbTextCompare)
Set pic = LoadPicture(img_path)
PicH = ScaleY(pic.Height, vbHimetric, vbPixels) \ 1
PicW = ScaleX(pic.Width, vbHimetric, vbPixels) \ 1
sectionH = PicH \ divide_Y
sectionW = PicW \ divide_X
PicH = PicH - sectionH
PicW = PicW - sectionW
For a = 0 To PicH Step sectionH
    For l = 0 To PicW Step sectionW
        Picture2.PaintPicture pic, 0, 0, sectionW, sectionH, l, a, _
                      sectionW, sectionH, vbSrcCopy
        iCount = iCount + 1
        SavePicture Picture2.Image, "c:\" & strRet(0) & iCount & ".bmp"
    Next l
Next a
End Function





0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6939680
This is another simplest posibility. Take care that some values are hard-coded:


VERSION 5.00
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
Object = "{27395F88-0C0C-101B-A3C9-08002B2F49FB}#1.1#0"; "PICCLP32.OCX"
Begin VB.Form frmPicSplit
   Caption         =   "Form1"
   ClientHeight    =   3195
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   4680
   LinkTopic       =   "Form1"
   ScaleHeight     =   3195
   ScaleWidth      =   4680
   StartUpPosition =   3  'Windows Default
   Begin MSComDlg.CommonDialog cdiag
      Left            =   2100
      Top             =   1350
      _ExtentX        =   847
      _ExtentY        =   847
      _Version        =   393216
      DefaultExt      =   "bmp"
      DialogTitle     =   "Image to split"
      FileName        =   "*.bmp"
   End
   Begin VB.TextBox txtSize
      Height          =   300
      Index           =   0
      Left            =   930
      TabIndex        =   0
      Top             =   75
      Width           =   450
   End
   Begin VB.TextBox txtSize
      Height          =   300
      Index           =   1
      Left            =   2385
      TabIndex        =   1
      Top             =   90
      Width           =   450
   End
   Begin VB.CommandButton Command1
      Caption         =   "&Split"
      Height          =   375
      Left            =   3045
      TabIndex        =   3
      Top             =   60
      Width           =   1215
   End
   Begin PicClip.PictureClip PClip
      Left            =   150
      Top             =   465
      _ExtentX        =   2646
      _ExtentY        =   1323
      _Version        =   393216
   End
   Begin VB.Label Label1
      Appearance      =   0  'Flat
      AutoSize        =   -1  'True
      BackColor       =   &H80000005&
      BackStyle       =   0  'Transparent
      Caption         =   "Divide X:"
      ForeColor       =   &H80000008&
      Height          =   195
      Index           =   0
      Left            =   105
      TabIndex        =   4
      Top             =   120
      Width           =   645
   End
   Begin VB.Label Label1
      Appearance      =   0  'Flat
      AutoSize        =   -1  'True
      BackColor       =   &H80000005&
      BackStyle       =   0  'Transparent
      Caption         =   "Divide Y:"
      ForeColor       =   &H80000008&
      Height          =   195
      Index           =   1
      Left            =   1560
      TabIndex        =   2
      Top             =   150
      Width           =   645
   End
End
Attribute VB_Name = "frmPicSplit"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private Sub Command1_Click()
Dim pCells As Integer
pCells = (txtSize(0).Text * txtSize(1).Text) - 1
With cdiag
    .ShowOpen
    PClip.Cols = txtSize(0).Text
    PClip.Rows = txtSize(1).Text
    PClip.Picture = LoadPicture(.FileName)
    Dim i As Integer
    For i = 0 To pCells
        SavePicture PClip.GraphicCell(i), "c:\image" & i & ".bmp"
    Next i
End With
End Sub

0
 

Author Comment

by:peterned
ID: 6939777
Richie,

the first example gives error in
Picture2.PaintPicture pic, 0, 0, sectionW, sectionH, l, a,_
     sectionW, sectionH, vbSrcCopy
line
The second one in
pclip.Cols = txtSize(0).Text
line

Should I add some referencies or something?
Also it's very important to save the pieces in the original file format, not always as .bmp
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Expert Comment

by:ProgramIT
ID: 6939819
Why not use the Microsoft PictureClip control.
A graphic can be loaded into it, then split using the Cols and Row properties.
Then by calling the GraphicCell properties a section of the picture can be used.
The cell then is treated like a seperate pic, which can be manipulated copied etc.
0
 

Author Comment

by:peterned
ID: 6939889
According to VB help only .bmp's can be loaded into PictureClip control, but I also need this to work for .jpg and .gif
0
 

Author Comment

by:peterned
ID: 6939936
Richie,

I did what I needed based on your two examples.
Thanx!
I'll now ask another  question named 'For Richie Simonetti', please post a reply and I'll accept it.
Thanks once again, you helped me a lot.

Peter
0
 

Author Comment

by:peterned
ID: 6939943
BTW there is no problem to save the pieces in the file format of the input file, not .bmp
0
 

Author Comment

by:peterned
ID: 6939948
about animated .gif's - if I split the file, the animation is lost, only the initial frame is splitted and the pieces are not animated.
I guess it's not possible to retain the animation in the pieces(I'm really lame in regard to images), but if it's possible please advice
0
 

Author Comment

by:peterned
ID: 6940060
Oh well,

I was completely wrong, nothing works :(

My default image viewer is Opera browser, which somehow displays the pieces of the splitted file.
But they can't be displayed in any other app, like IE5 or Photoshop.
So my question remains.
Since I already accepted an answer, if a working answer appears I'll ask this question again and will accept the really working answer for all the points I have now (nearly 500)
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6940517
Ok. There is a bug in my code so take note of this modifications:

Function split_img(img_path As String, divide_X As Integer, divide_Y As Integer)
Dim pic As StdPicture
Dim PicH As Long, PicW As Long
Dim sectionH As Long, sectionW As Long
Dim l As Integer, a As Integer, iCount As Integer
Dim filename As String, strRet As String

strRet = Mid$(img_path, InStrRev(img_path, "\", , vbTextCompare) + 1)
filename = Left$(strRet, InStr(1, strRet, ".", vbTextCompare) - 1)
Set pic = LoadPicture(img_path)
PicH = ScaleY(pic.Height, vbHimetric, vbPixels) \ 1
PicW = ScaleX(pic.Width, vbHimetric, vbPixels) \ 1
sectionH = PicH \ divide_Y
sectionW = PicW \ divide_X
PicH = PicH - sectionH
PicW = PicW - sectionW
For a = 0 To PicH Step sectionH
    For l = 0 To PicW Step sectionW
        With Picture1
            .ScaleWidth = sectionW
            .ScaleHeight = sectionH
            .PaintPicture pic, 0, 0, sectionW, sectionH, l, a, _
                      sectionW, sectionH, vbSrcCopy
            iCount = iCount + 1
            SavePicture .Image, "c:\" & filename & iCount & ".bmp"
            .Picture = LoadPicture("")
        End With
    Next l
Next a
End Function

Thanks for "A" grade.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now