Link to home
Start Free TrialLog in
Avatar of zavikon
zavikon

asked on

How to Decode Jpeg to Bitmap and Encode Bitmap to Jpeg

In my code block below, I am opening a jpeg image and converting it to a bitmap.  I am then modifying the bitmap and then saving it back to a jpeg.  This works, however, when I go to convert my new jpeg to a pdf file, I get an error that the file was not decoded/encoded properly and the conversion cannot occur.  So, it seems that I am not properly decoding the JPEG prior to creating the bitmap and then enconding the bitmap back to a jpeg properly.  Anyone have any idea how to do this?

sSourceAbsFilePath = "C:\autos\photo_source\" & sImageLocation
sDestAbsFilePath = "C:\autos\photo_new\" & sImageLocation
sTextLine = "VIN No:"

Dim oFont As Drawing.Font = New Drawing.Font("Calibri", 36, FontStyle.Regular, GraphicsUnit.Pixel)
                    'read jpg to bitmap
                    Dim oBitmap As Bitmap = Bitmap.FromFile(sSourceAbsFilePath)
                    'create new bitmap with border at bottom
                    Dim oNewBitmap As Bitmap = New Bitmap(oBitmap.Width, oBitmap.Height + 100)
                    'create new image with oversized canvas
                    Dim oGraphic As Graphics = Graphics.FromImage(oNewBitmap)
                    'draw original image on new canvas
                    oGraphic.DrawImageUnscaled(oBitmap, 0, 0)
                    oGraphic.DrawString(sTextLine, oFont, Brushes.Red, New Rectangle(0, oBitmap.Height, oBitmap.Width, 100))
                    oNewBitmap.Save(sDestAbsFilePath)

Open in new window


Thanks,
ASKER CERTIFIED SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands 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
Avatar of zavikon
zavikon

ASKER

You are right, I added the format to the save and not it works without a problem!