Solved

How to Decode Jpeg to Bitmap and Encode Bitmap to Jpeg

Posted on 2013-06-21
2
1,185 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Change data in datatable 8 30
Get String split 5 47
Help with creating multiple xml files from one xml file using VB.NET 6 38
Help with AsEnumerable(), LINQ 4 22
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

22 Experts available now in Live!

Get 1:1 Help Now