Solved

How to Decode Jpeg to Bitmap and Encode Bitmap to Jpeg

Posted on 2013-06-21
2
1,208 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 39267835
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
ID: 39267947
You are right, I added the format to the save and not it works without a problem!
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

717 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