Home > Mobile >  Convert a Bitmap to a 1bit Monochrome/Black and white Bitmap VB.net
Convert a Bitmap to a 1bit Monochrome/Black and white Bitmap VB.net

Time:09-28

I have a BMP file.. a basic barcode image.. When I go to save it Bitmap.save(File) it saves the BMP file all fine, but when I open it in photoshop its a indexed or RGB color bmp files.

I need the file to be a 1bit black and white bmp file. Its just a barcode...

I have been googling for hours, and I am unable to convert the c# examples (often uses Unsafe code) and all other examples are also just creating what photoshop sees as a RGB image.

I need the file to be 1bit bmp as its loaded onto a ePaper device (like a kindle reader) and the device supplier has provided sample bitmaps that when loaded into photoshop as a mode Bitmap... not RGB or indexed.

Any tips on how to get the file saved correctly ?

HPD

Here is the code i was working with...

    Dim original = New Bitmap("c:\temp\24.bmp")
    Dim Rectangle = New Rectangle(0, 0, original.Width, original.Height)
    Dim bmp1bpp = original.Clone(Rectangle, PixelFormat.Format1bppIndexed)
    bmp1bpp.Save("c:\temp\24bit1.bmp")

CodePudding user response:

Thanks for all the help.. I did manage to get this all working now...

I have another question related, but differet so guess i will create a new question for this..

Again.. Thanks

CodePudding user response:

here is sample conversion code in c#:

private static ImageCodecInfo GetEncoder(ImageFormat format)
{
    ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();

    foreach (ImageCodecInfo codec in codecs) {
        if (codec.FormatID == format.Guid) {
            return codec;
        }
    }

    return null;
}

static void Main(string[] args)
{
    var input = new Bitmap(@"d:\test.bmp");
    var output = input.Clone(
        new Rectangle(0, 0, input.Width, input.Height),
        PixelFormat.Format1bppIndexed);

    var encoder = GetEncoder(ImageFormat.Bmp);
    var param = new EncoderParameter(Encoder.ColorDepth, 1L);
    var paramss = new EncoderParameters(1);
    paramss.Param[0] = param;

    output.Save(@"d:\test_out.bmp", encoder, paramss);
}

and it produces bitmap mode bmp

here is conversion to vb:

Imports System.Drawing
Imports System.Drawing.Imaging

Module Module1
    Function GetEncoder(ByVal format As ImageFormat) As ImageCodecInfo
        Dim codecs = ImageCodecInfo.GetImageEncoders()

        For Each codec As ImageCodecInfo In codecs
            If codec.FormatID = format.Guid Then
                Return codec
            End If
        Next

        Return Nothing
    End Function

    Sub Main()
        Dim input = New Bitmap("d:\test.bmp")
        Dim output = input.Clone(
            New Rectangle(0, 0, input.Width, input.Height),
            PixelFormat.Format1bppIndexed)

        Dim encoder = GetEncoder(ImageFormat.Bmp)
        Dim param = New EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, 1L)
        Dim paramss = New EncoderParameters(1)
        paramss.Param(0) = param

        output.Save("d:\test_out.bmp", encoder, paramss)
    End Sub

End Module
  • Related