Resize your pictures for your phone or pocket pc


I have many digital pictures, and I’d like to put some of my favorites on my SmartPhone. However the files are much higher resolution than the device’s display. This means the files take a lot more memory on the phone, and they take longer to process. I wrote some code to resize the images automatically. The code below shows a form with a resized picture on it, and you can vary the Quality of the JPG saved image by moving a TrackBar control. The form’s caption shows the Quality selected, and the resulting file size. It’s amazing how small you can make the file without losing too much quality.


 


Start Visual Studio (2005 or 2008), choose File->New->Project->Windows->Windows Forms Application. Choose View->Code and paste in the VB code below. Run it and give it a digital picture to play with.


 


The code creates oBitmap as an Image object, representing the original image. oBmpOut is an Image object, with the desired target size. A Graphics object is created from oBmpOut and oBitmap is drawn onto that Graphics object. Then the JPG Image encoder is found, and the JPG is saved with the specified Quality.


 


Below is sample code in both VB.Net and Fox that shows a particular JPG resampled to 320 x 240 resolution, with a TrackBar that allows you to change the JPG quality.


 


See also:


Recompress your digital pictures to save space


Various ways to display multiple photographs


 


Start of Fox code


 


PUBLIC ox as JpgForm


ox=CREATEOBJECT(“JpgForm”)


ox.visible=1


DEFINE CLASS JpgForm as Form


          width=640


          height=480


          left=200


          allowoutput=.f.


          ADD OBJECT oPict as image WITH top = 20,width=320,height=240,stretch=2


          ADD OBJECT oTrackbar as cTrackbar WITH width=300,smallchange=2


          PROCEDURE init


                   SET CLASSLIB TO HOME()+”ffc\_gdiplus”


                   SET COMPATIBLE on  && so FSIZE returns FileSize


                   this.refresh


          PROCEDURE refresh


                   this.drawit()


          PROCEDURE drawit


                   cFile=”d:\kids.jpg”


                   cOutputFile=”d:\thumb.jpg”


                   LOCAL oGraphics as gpGraphics OF _gdiplus.vcx


                   LOCAL oImage as gpImage OF _gdiplus.vcx


                   LOCAL oBmpOut as gpBitmap OF _gdiplus.vcx


                   oGraphics=CREATEOBJECT(“gpGraphics”)


                   oImage=CREATEOBJECT(“gpImage”,cFile)


                   oBmpOut=CREATEOBJECT(“gpBitmap”,this.oPict.width,this.oPict.height)


                   oGraphics.CreateFromImage(oBmpOut)


                   oGraphics.DrawImageScaled(oImage,0,0,oBmpOut.ImageWidth,oBmpOut.ImageHeight)


                   oBmpOut.SaveToFile(cOutputFile,”image/jpeg”,”quality=”+TRANSFORM(this.oTrackbar.value))


                   this.oPict.picture=cOutputFile


                   this.Caption = TRANSFORM(this.oTrackbar.value)+” Size=”+TRANSFORM(FSIZE(cOutputFile))


ENDDEFINE


DEFINE CLASS cTrackbar as olecontrol


          oleclass=”mscomctllib.slider.2″


          PROCEDURE init


                   this.max=100


                   this.smallchange=2


                   this.largechange=10


          PROCEDURE change


                   thisform.drawit


ENDDEFINE


 


 


End of Fox code


 


 


 


Start of VB code:


 


Public Class Form1


 


    Dim oPict As New PictureBox


    Dim WithEvents oTrackBar As New TrackBar


    Dim cFile As String = “d:\kids.jpg”


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        Me.Width = 640


        Me.Height = 480


        oPict.Top = 30


        oPict.Width = 640


        oPict.Height = 480


        oPict.Visible = 1


        Dim OFD As New OpenFileDialog


        OFD.FileName = cFile


        If OFD.ShowDialog <> Windows.Forms.DialogResult.OK Then


            End


        End If


        cFile = OFD.FileName


        Me.Controls.Add(oPict)


        oTrackBar.Width = 300


        oTrackBar.Height = 20


        oTrackBar.Visible = 1


        oTrackBar.Minimum = 0


        oTrackBar.Maximum = 100


        oTrackBar.SmallChange = 5


        oTrackBar.Value = 50


        Me.Controls.Add(oTrackBar)


    End Sub


    Sub TrackBar_Change(ByVal sender As Object, ByVal e As System.EventArgs) Handles oTrackBar.ValueChanged


        CalcPict()


    End Sub


    Sub CalcPict()


        Dim oBitmap As Bitmap = New Bitmap(cFile)


        Dim oBmpOut As Bitmap = New Bitmap(Me.oPict.Width, Me.oPict.Height)


        Dim oG As Graphics = Graphics.FromImage(oBmpOut)


        oG.DrawImage(oBitmap, 0, 0, oBmpOut.Width, oBmpOut.Height)


        Dim encParam As New Imaging.EncoderParameters(1)


        encParam.Param(0) = New Imaging.EncoderParameter(Imaging.Encoder.Quality, oTrackBar.Value)


 


        Dim codecs As Imaging.ImageCodecInfo() = Imaging.ImageCodecInfo.GetImageEncoders


        Dim JpgCodec As Imaging.ImageCodecInfo = Nothing


        For Each codec As Imaging.ImageCodecInfo In codecs


            If codec.MimeType = “image/jpeg” Then


                JpgCodec = codec


                Exit For


            End If


        Next


        Dim istream As New IO.MemoryStream


 


        oBmpOut.Save(istream, JpgCodec, encParam)


        oPict.Image = New Bitmap(istream)


        Me.Text = oTrackBar.Value.ToString + ” Size=” + istream.Length.ToString


 


    End Sub


End Class


 


End of VB code:


 


 


 

Comments (9)

  1. I was running out of space again on my notebook. As years go by, digital cameras can take pictures with

  2. I was running out of space again on my notebook. As years go by, digital cameras can take pictures with