?
Solved

Convert color JPG image to BW image via .net

Posted on 2009-04-24
6
Medium Priority
?
646 Views
Last Modified: 2012-05-06
Hi.

We have a number of images that are dynamicaly displayed on a site. We need to show a greyscale version of the images. I've seen a number of ways for bmp's but none for jpg's.

It's a vb.net framework 2 site.

Thanks.
0
Comment
Question by:QuestionManA
  • 4
  • 2
6 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24228227
If you have a JPG, you can turn it into a BMP by using the Graphics library. Just say:

Bitmap someBitmap = new Bitmap("mypicture.jpg");
which will automatically create an internal bitmapped image for you. You can access it for manipulation with:

Graphics graphicsFromAJpeg = Graphics.FromImage(someBitmap);
0
 
LVL 39

Expert Comment

by:abel
ID: 24228236
FYI, from the microsoft documentation, on the constructor above it says:

"Use this constructor to open images with the following file formats: BMP,  GIF, EXIG, JPG, PNG and TIFF. For more information about supported formats, see  Types of  Bitmaps. The file remains locked until the Bitmap is disposed. "
0
 
LVL 2

Author Comment

by:QuestionManA
ID: 24228392
Is converting to a bmp a requirement to create a greyscale version of the image?
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 39

Accepted Solution

by:
abel earned 2000 total points
ID: 24228713
The conversion itself it pretty straightforward, but depending on what you are after (simple solution and slow or fast solution and complex) I can recommend the following link: http://www.switchonthecode.com/tutorials/csharp-tutorial-convert-a-color-image-to-grayscale

The solution offered there takes a Bitmap like I showed above. All the necessary conversion will then be done by GDI+ (the graphics library of .NET).

If you want to save it as a bitmap (BMP) you can do that, too. Or you can save it back again as a JPG:



// open and convert jpg image to internal bitmap
Bitmap originalBitmap = new Bitmap("mypicture.jpg");
 
// using the method form the link I showed
Bitmap grayscaleBitmap = MakeGrayscale(Bitmap original);
 
// save result as greyscale image as JPEG
someBitmap.Save("c:\\greyScaleImage.jpg", ImageFormat.Jpeg);
 
// save result as greyscale image as BMP
someBitmap.Save("c:\\greyScaleImage.bmp", ImageFormat.Bmp);

Open in new window

0
 
LVL 39

Expert Comment

by:abel
ID: 24228723
sorry, mistake with my copy and paste, this will work

// open and convert jpg image to internal bitmap
Bitmap originalBitmap = new Bitmap("mypicture.jpg");
 
// using the method form the link I showed
Bitmap grayscaleBitmap = MakeGrayscale(originalBitmap);
 
// save result as greyscale image as JPEG
grayscaleBitmap.Save("c:\\greyScaleImage.jpg", ImageFormat.Jpeg);
 
// save result as greyscale image as BMP
grayscaleBitmap.Save("c:\\greyScaleImage.bmp", ImageFormat.Bmp);

Open in new window

0
 
LVL 2

Author Closing Comment

by:QuestionManA
ID: 31574369
Perfect.  That is exactly what I needed. Thank you.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

850 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