Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Splitting gif/jpg file into pieces

Posted on 2002-04-14
Medium Priority
Last Modified: 2013-11-19

I need to split an image file(gif or jpg) into pieces.
If there is a file like img.gif, I want to split it into files named img1.gif...img4.gif, all having the same image size = img.gif_width/2  x img.gif_heigth/2
The input file extension should be preserved in the pieces.

Question by:peterned
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
  • 13
  • 5
  • 3
  • +4

Expert Comment

ID: 6940288
i think, i've seen the same type of question somewhere in EE.

seems to be very difficult.

LVL 16

Expert Comment

ID: 6940538
To save as gif format you need too money.
To save as jpg there is a free dll you could use.
I have to search the url.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 16

Expert Comment

ID: 6940555

Author Comment

ID: 6940611
thanx, i'll see these
using your examples in my other question, I was able to split .jpg and .gif into pieces and save them in the same format.
The Opera browser displays the pieces just fine, but IE, Netscape, Photoshop etc can't - this means (as I think) the image data is OK but something in the file (which Opera ignores) should be different. If I open the input image with hex editor(ultraedit), it starts with
Saved pieces start with

Expert Comment

ID: 6940955
Just in case it hasn't been mentioned earlier...
look at the PicClip control.  You could divvy up the picture any way you want (in rectangles), and save the resultant pieces as separate images.  Or use them as they are created - up to you.
LVL 16

Expert Comment

ID: 6941022
I used that from vbaccelerator without any problem.
I am still not able to resolve gif matter but for jpg, you could try this:
' Picture needs to set scalemode to pixels as a warranty.
Option Explicit
Private Type imgdes  ' Image descriptor
  ibuff As Long
  stx As Long
  sty As Long
  endx As Long
  endy As Long
  buffwidth As Long
  palette As Long
  colors As Long
  imgtype As Long
  bmh As Long
  hBitmap As Long
End Type

' Victor functions
Private Declare Function allocimage Lib "VIC32.DLL" (image As imgdes, ByVal wid As Long, ByVal leng As Long, ByVal BPPixel As Long) As Long
Private Declare Sub freeimage Lib "VIC32.DLL" (image As imgdes)
Private Declare Function rainbowpalette Lib "VIC32.DLL" (image As imgdes) As Long
Private Declare Function savegif Lib "VIC32.DLL" (ByVal Fname As String, srcimg As imgdes) As Long
Private Declare Function savejpg Lib "VIC32.DLL" (ByVal Fname As String, srcimg As imgdes, ByVal quality As Long) As Long
Private Declare Function updatebitmapcolortable Lib "VIC32.DLL" (image As imgdes) As Long

' Windows functions
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC 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 dwRop As Long) As Long
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long

' VB function to save a picture box to JPEG and GIF files
Private Sub savePicBox()
Dim rcode, dummy As Long
Dim tempimage As imgdes
Dim hMemDC As Long
Dim hOldBitmap As Long
Dim width, height, vbitcount As Long
Const SRCCOPY = &HCC0020

'' Save contents of Picture1, picture box control to a JPEG file
'vbitcount = 24        ' Use 24-bit for JPEG, use 8-bit for GIF
'width = Picture1.ScaleWidth
'height = Picture1.ScaleHeight
'hMemDC = CreateCompatibleDC(Picture1.hdc)
'' Allocate the buffer to hold the image
'rcode = allocimage(tempimage, width, height, vbitcount)
'If (rcode = 0) Then
'     ' Select our image buffer into the memory device context
'     ' all subsequent operations on the device context alter
'     ' the pixels in the image buffer.
'    hOldBitmap = SelectObject(hMemDC, tempimage.hBitmap)
'     ' The bitblt copies pixel data from the picture box into the image buffer
'    dummy = BitBlt(hMemDC, 0, 0, width, height, Picture1.hdc, 0, 0, SRCCOPY)
'     ' Save the image
'    rcode = savejpg("c:\newimage.jpg", tempimage, 75)
'End If
' ' We're done, so release the device context and free the image buffer
'hOldBitmap = SelectObject(hMemDC, hOldBitmap)
'dummy = DeleteDC(hMemDC)
'freeimage tempimage

' Save contents of Picture1, picture box control to a GIF file
' Same procedure as for the JPEG, but we allocate an 8-bit buffer,
' create a rainbow palette, and call savegif.

vbitcount = 8
width = Picture1.width
height = Picture1.height
hMemDC = CreateCompatibleDC(Picture1.hdc)
rcode = allocimage(tempimage, width, height, vbitcount)
If (rcode = 0) Then
    dummy = rainbowpalette(tempimage)
    dummy = updatebitmapcolortable(tempimage)
    hOldBitmap = SelectObject(hMemDC, tempimage.hBitmap)
    dummy = BitBlt(hMemDC, 0, 0, width, height, Picture1.hdc, 0, 0, SRCCOPY)
    rcode = savegif("c:\newimage.gif", tempimage)
End If

hOldBitmap = SelectObject(hMemDC, hOldBitmap)
dummy = DeleteDC(hMemDC)
freeimage tempimage

End Sub

Private Sub Command1_Click()
End Sub

mcoop, that idea was already presented in other task regarding peterned's problem.
LVL 16

Expert Comment

ID: 6941024
oops, remove the comment sign if not you could not save the jpg format.

Author Comment

ID: 6941299

it didn't work. When I tried to register vic32.dll, an error like 'DLLRegisterServer entry point not found' was displayed (i run w2k pro sp1).
I tried to save gif as gif, bmp as gif/jpg and jpg as jpg, but in all cases the saved image was corrupted - when viewed in Opera, it looks like a capture of a part of the screen, not a part of the input image. This is also quite expensive.
Today will try other links you posted

Author Comment

ID: 6941367
I tried the example from freevbcode about saving .bmp as .gif - works, but the .gif quality is very bad compared to the input .bmp
LVL 16

Expert Comment

ID: 6942011
It depends upon how many bit has bmp file, gif format allows only 256 colors.
That dll is no a COM dll, just plain dll, you have not to register it.
Let me know....
LVL 22

Expert Comment

ID: 6943009
What I'd do is load the image into a picturebox (or form) then move the pieces into a separate picturebox to be saved and repeat for all four pieces:

' Assum original picture is in picOriginal, and picTemp holds the piece
For iRow=0 to iRowCount-1
  For iColumn=0 to iColumnCount-1
    iLeftStartPixel=iRow * picOriginal.width/iRowCount

    picTemp.Picture = LoadPicture("")
    picTemp.PaintPicture picOriginal.Picture, 0,0,picOriginal.width/iRowCount, picOriginal.height/iColumnCount, iLeftStartPixel, iTopStartPixel
    SaveImagePiece iRow *iColumnCount+iColumn
  next iColumn
next iRow

Private sub SaveImagePiece(IDNumber as integer)
  ' This routine will store the picture in picTemp
  ' to store as a bitmap:
  picTemp.SavePicture("PicPiece" & IDNumber & ".bmp")
  ' For other formats, you'll need a special converter
end sub
LVL 22

Expert Comment

ID: 6943010
Sorry..you need to put the clip size in the PaintPicture:

picTemp.PaintPicture picOriginal.Picture, 0,0, picOriginal.width/iRowCount, picOriginal.height/iColumnCount,iLeftStartPixel, iTopStartPixel, 0,0, picOriginal.width/iRowCount, picOriginal.height/iColumnCount
LVL 16

Expert Comment

ID: 6943078
Hi rspahitz,
the problem is not spliting the image (it was already achieved)
The major problem is how to save splitted images in the same format as source image.
Peterned, i did use Vic32.dll and it works but, as i said, only for jpg format, i really don't know why doesn't works for gif.
LVL 22

Expert Comment

ID: 6943107
OK, Richie.  However, I offered the preferred VB6 method for manipulating pictures:  PatinPicture (which internally uses BitBlT but includes error-handling.)  This also takes much less code and is therefore less subject to error.

LVL 16

Expert Comment

ID: 6943136
peterned, i found location for vic32.dll:
rspahitz, thats OK, it was done in another thread with code similar to yours.
LVL 16

Expert Comment

ID: 6943139
LVL 16

Expert Comment

ID: 6943170
Well, i have been trying with vic32.dll and get both files(Gif and JPG) from an image stored in a picture box Under windows 2000 with vb6 sp5

Author Comment

ID: 6945110
well i still can't make vic32.dll to work.
I solved the problem to save pieces as jpg with intel's jpeg library ijl11.dll
but still can't find a way to save as gif. There is a working solution at freevbcode but the quality of the saved image is awful and seems there is no way to fix this
LVL 16

Expert Comment

ID: 6945236
Just remember that if source image is in 24 bit and you save it in gif format, quality degradation will happen.
Which is the problem with vic32.dll?
I just downloaded it and run it over w2k and images are saved without problem.

Author Comment

ID: 6945884
the problem is saved images can't be viewed anywhere but in Opera browser. In Opera, they look strange, least to say. In IE & NN they appear as red 'x'. Image editing apps like photoshop throw errors when trying to open such images.
I tried this on 3 PC's, so this is not a problem of a particular PC only
all the above is for vic32.dll, I'll not try anymore with it
this piece of code
can save whatever as gif, but with an auful quality. Any idea how to make it work better?
LVL 16

Expert Comment

ID: 6945988
Strange, i can open those images generated by vic with Paint shop pro and IE.
LVL 16

Accepted Solution

Richie_Simonetti earned 1200 total points
ID: 6946094
Well, i did try the code from Ark and you are right, quality suffers too much even for a 256 color depth.
Well, a time ago, i was playing with www.vbaccelerator.com/vb-media which, among others things, has the ability to change palettes to files. It is just a guess but i think that is the problem with low quality in code from freevbcode.
I will do a test tonight.
By the way, do you want to see my generated images with vic? i could send original and a gif/jpg to you to see if you could see them normally.
LVL 16

Expert Comment

ID: 7755053

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in Community Support that this question is:
- points to Richie_Simonetti
Please leave any comments here within the
next seven days.

Expert Comment

ID: 7812465
per recommendation

Community Support Moderator @Experts Exchange

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Learn how to create interesting presentations by including videos to keep your audience engaged using Prezi. Select "Insert" from the top menu in your Prezi editor: Select "YouTube Video": Paste the video URL into the prompt: "Select "Insert":…
It’s easy to embed any of your public Prezi presentations on your website or social network to share with others. Learn how simple it is in this tutorial.
Suggested Courses

610 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