Solved

Splitting gif/jpg file into pieces

Posted on 2002-04-14
25
512 Views
Last Modified: 2013-11-19
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
0
Comment
Question by:peterned
  • 13
  • 5
  • 3
  • +4
25 Comments
 
LVL 5

Expert Comment

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

seems to be very difficult.

0
 
LVL 16

Expert Comment

by:Richie_Simonetti
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.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6940544
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6940555
0
 

Author Comment

by:peterned
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
ÿØÿà JFIF
Saved pieces start with
BMv~
0
 
LVL 4

Expert Comment

by:mcoop
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.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6941022
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.
0
 
LVL 16

Expert Comment

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

Author Comment

by:peterned
ID: 6941299
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
0
 

Author Comment

by:peterned
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
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
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....
0
 
LVL 22

Expert Comment

by:rspahitz
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
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
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 22

Expert Comment

by:rspahitz
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
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
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.
0
 
LVL 22

Expert Comment

by:rspahitz
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.


0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6943136
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.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6943139
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
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
0
 

Author Comment

by:peterned
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
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
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.
0
 

Author Comment

by:peterned
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
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?
0
 
LVL 16

Expert Comment

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

Accepted Solution

by:
Richie_Simonetti earned 300 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.
0
 
LVL 16

Expert Comment

by:twalgrave
ID: 7755053
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.
0
 

Expert Comment

by:SpideyMod
ID: 7812465
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I've been using this technique since Adobe CS2, and it should work with any version of Illustrator that includes the appearance panel. In this tutorial we'll create a button using the appearance panel in Adobe Illustrator, and then save it as a r…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Learn the basics of inputting and editing your text components in Prezi. We will cover how to set styles, position, and group your text components. In your Prezi editor, click anywhere on the canvas to add text: A flashing cursor informs you that yo…
In addition to being a great web-based presentation tool, Prezi also makes it easy to save your presentation as a PDF to share with others as well. Learn how in this tutorial. Select the share icon from the top menu in your Prezi editor: Select "D…

746 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

11 Experts available now in Live!

Get 1:1 Help Now