Solved

How to Decode Jpeg to Bitmap and Encode Bitmap to Jpeg

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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