Solved

How to Decode Jpeg to Bitmap and Encode Bitmap to Jpeg

Posted on 2013-06-21
2
1,176 Views
Last Modified: 2013-06-22
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,
0
Comment
Question by:zavikon
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
Comment Utility
The first part seems ok but on saving you need to specify the format, it's not enough to specify a .jpg filename:
oNewBitmap.Save(sDestAbsFilePath, Imaging.ImageFormat.Jpeg)

Open in new window

0
 

Author Comment

by:zavikon
Comment Utility
You are right, I added the format to the save and not it works without a problem!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

13 Experts available now in Live!

Get 1:1 Help Now