Link to home
Start Free TrialLog in
Avatar of peterned
peterned

asked on

Splitting gif/jpg file into pieces

Hi,

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.




thanks
Avatar of Chandramouli k
Chandramouli k
Flag of India image

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

seems to be very difficult.

Avatar of Richie_Simonetti
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.
Avatar of peterned
peterned

ASKER

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
ÿØÿà JFIF
Saved pieces start with
BMv~
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.
peterned:
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()
savePicBox
End Sub




mcoop, that idea was already presented in other task regarding peterned's problem.
oops, remove the comment sign if not you could not save the jpg format.
Richie,

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
I tried the example from freevbcode about saving .bmp as .gif - works, but the .gif quality is very bad compared to the input .bmp
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....
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
iRowCount=2
iColumnCount=2
For iRow=0 to iRowCount-1
  For iColumn=0 to iColumnCount-1
    iLeftStartPixel=iRow * picOriginal.width/iRowCount
    iTopStartPixel=iColumn*picOriginal.height/iColumnCount

    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
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
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.
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.


peterned, i found location for vic32.dll:
http://www.catenary.com/download/vic5e.html
rspahitz, thats OK, it was done in another thread with code similar to yours.
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
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
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.
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
http://www.freevbcode.com/ShowCode.Asp?ID=3533
can save whatever as gif, but with an auful quality. Any idea how to make it work better?
Strange, i can open those images generated by vic with Paint shop pro and IE.
ASKER CERTIFIED SOLUTION
Avatar of Richie_Simonetti
Richie_Simonetti
Flag of Argentina image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

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.
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange