Solved

Save Image as PNG - Grayscale 8bit

Posted on 2010-11-08
3
2,915 Views
Last Modified: 2012-05-10
I need to save an Image as an 8bit GrayScale PNG file in VB.NET

To verify that the format is correct, the header information for the saved PNG file is:

byte 0x00: 0x89
byte 0x01: 0x50
byte 0x02: 0x4E

byte 0x18: 0x08      '8bit
byte 0x19: 0x00      'GrayScale


I can get 8 bit using PixelFormat.Format8bppIndexed, but this does not give me gray-scale.
0
Comment
Question by:Ola3
  • 2
3 Comments
 
LVL 15

Expert Comment

by:x77
ID: 34086327
You can compute each bit for grayscale. Note that you need use a factor for each R G B component.

You can use GDI API Functions:
 

1. Create an 1 plane 8 bits/pixel device independent bitmap using CreateDIBSection.
2. Set its color table to greyscale colors.
3. Use BitBlt to transfer your color bitmap into this one. Windows will make the necessary conversions automatically and the bitmap will appear in greyscale.

You can find many samples on internet:
http://efreedom.com/Question/1-2517477/Converting-24BPP-4BPP-GDI-VBNET
0
 
LVL 32

Accepted Solution

by:
Erick37 earned 500 total points
ID: 34086893
Give this a shot.
Option Explicit On
Option Strict On

Imports System.Drawing.Imaging
Imports System.Windows.Media.Imaging
Imports System.IO

Public Class Form1

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

		''Load original image
		Dim image1 As Bitmap = New Bitmap("c:\input.png")

		'PictureBox1.Image = image1

		'Convert to Grayscale
		Dim image2 As Bitmap = MakeGrayscale(image1)

		'PictureBox2.Image = image2

		'Save as PNG, 8bbp
		Dim fcb As FormatConvertedBitmap = New FormatConvertedBitmap(System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(image2.GetHbitmap(System.Drawing.Color.Transparent), IntPtr.Zero, System.Windows.Int32Rect.Empty, _
		  BitmapSizeOptions.FromWidthAndHeight(image2.Width, image2.Height)), System.Windows.Media.PixelFormats.Gray8, BitmapPalettes.Gray256, 0.5)
		Dim pngBitmapEncoder As PngBitmapEncoder = New PngBitmapEncoder()
		pngBitmapEncoder.Interlace = PngInterlaceOption.Off
		pngBitmapEncoder.Frames.Add(BitmapFrame.Create(fcb))
		Dim fileStream As Stream = File.Open("c:\output.png", FileMode.Create)
		pngBitmapEncoder.Save(fileStream)
		fileStream.Close()

	End Sub

	Public Function MakeGrayscale(ByVal original As Bitmap) As Bitmap

		''create a blank bitmap the same size as original
		Dim newBitmap As Bitmap = New Bitmap(original.Width, original.Height)

		''get a graphics object from the new image
		Dim g As Graphics = Graphics.FromImage(newBitmap)

		'create the grayscale ColorMatrix
		Dim colorMatrix As ColorMatrix = New ColorMatrix(New Single()() { _
		  New Single() {0.3F, 0.3F, 0.3F, 0, 0}, _
		  New Single() {0.59F, 0.59F, 0.59F, 0, 0}, _
		  New Single() {0.11F, 0.11F, 0.11F, 0, 0}, _
		  New Single() {0, 0, 0, 1, 0}, _
		  New Single() {0, 0, 0, 0, 1}})

		'create some image attributes
		Dim attributes As ImageAttributes = New ImageAttributes()

		'set the color matrix attribute
		attributes.SetColorMatrix(colorMatrix)

		'draw the original image on the new image using the grayscale color matrix
		g.DrawImage(original, New Rectangle(0, 0, original.Width, original.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes)

		'dispose the Graphics object
		g.Dispose()
		Return newBitmap

	End Function

End Class

Open in new window

0
 
LVL 32

Expert Comment

by:Erick37
ID: 34088011
Just realized in the sample above, the call to MakeGrayscale() is not needed since the FormatConvertedBitmap will do the conversion.

''Load original image
Dim image2 As Bitmap = New Bitmap("c:\input.png")

'Save as PNG, 8bbp
Dim fcb As FormatConvertedBitmap = ...
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb.net 2 43
Get the selected ValueMember of Combobox 5 30
VB.Net - KeyPress Event 4 36
start a process from a service 3 22
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

776 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