none
colorized old photo RRS feed

All replies

  • How can i colorized old black and white photo with vb.net? and how can i work 8 bit color bmp with color palets  in Vb.net?

    Hi,

    How do you want to colorize the photo?

    What do you want to do with the 8bpp bitmap? What do you mean by "work with 8bpp bitmap"?

    Do you just want to change the palette? If so, here's a copyPaste example for a WindowsForms Form that crreates a 8bpp bitmap and changes the palette on button click:

    Option Strict On
    
    Partial Public Class Form1
        Inherits Form
        Private pictureBox1 As System.Windows.Forms.PictureBox
        Private button1 As System.Windows.Forms.Button
        Private button2 As System.Windows.Forms.Button
    
        Public Sub New()
            InitializeComponent()
            'usually the controls are added by the designer, here in code for easy copy/paste
            Init()
        End Sub
    
        Private Sub Init()
            Me.pictureBox1 = New System.Windows.Forms.PictureBox()
            Me.button1 = New System.Windows.Forms.Button()
            Me.button2 = New System.Windows.Forms.Button()
            ' 
            ' pictureBox1
            ' 
            Me.pictureBox1.Location = New System.Drawing.Point(12, 12)
            AddHandler Me.pictureBox1.MouseMove, AddressOf PictureBox1_MouseMove
            Me.pictureBox1.Name = "pictureBox1"
            Me.pictureBox1.Size = New System.Drawing.Size(543, 464)
            Me.pictureBox1.TabIndex = 0
            Me.pictureBox1.TabStop = False
            ' 
            ' button1
            ' 
            Me.button1.Location = New System.Drawing.Point(26, 503)
            Me.button1.Name = "button1"
            Me.button1.Size = New System.Drawing.Size(75, 23)
            Me.button1.TabIndex = 1
            Me.button1.Text = "create"
            Me.button1.UseVisualStyleBackColor = True
            AddHandler Me.button1.Click, AddressOf Me.button1_Click
            ' 
            ' button2
            ' 
            Me.button2.Location = New System.Drawing.Point(140, 503)
            Me.button2.Name = "button2"
            Me.button2.Size = New System.Drawing.Size(75, 23)
            Me.button2.TabIndex = 1
            Me.button2.Text = "setPalette"
            Me.button2.UseVisualStyleBackColor = True
            AddHandler Me.button2.Click, AddressOf Me.button2_Click
    
            Me.ClientSize = New System.Drawing.Size(567, 538)
            Me.Controls.Add(Me.button2)
            Me.Controls.Add(Me.button1)
            Me.Controls.Add(Me.pictureBox1)
            Me.Name = "Form1"
            Me.Text = "Form1"
        End Sub
    
        'displays the color in the title bar
        Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs)
            If Me.pictureBox1.Image IsNot Nothing Then
                If e.X >= 0 AndAlso e.Y >= 0 AndAlso e.X < Me.pictureBox1.Image.Width AndAlso e.Y < Me.pictureBox1.Image.Height Then
                    Me.Text = DirectCast(Me.pictureBox1.Image, Bitmap).GetPixel(e.X, e.Y).ToString()
                End If
            End If
        End Sub
    
        Private _rnd As New Random()
    
        'creates an 8-bit bitmap gray
        Private Sub button1_Click(sender As Object, e As EventArgs)
            Dim bmp As New Bitmap(512, 512, System.Drawing.Imaging.PixelFormat.Format8bppIndexed)
    
            'map pixels to colors...
            Dim bitmapData As System.Drawing.Imaging.BitmapData = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.[WriteOnly], System.Drawing.Imaging.PixelFormat.Format8bppIndexed)
    
            Dim p As IntPtr = bitmapData.Scan0
    
            For y As Integer = 0 To bmp.Height - 1
                For x As Integer = 0 To bmp.Width - 1
                    System.Runtime.InteropServices.Marshal.WriteByte(p, y * bitmapData.Stride + x, CByte(_rnd.[Next](256)))
                Next
            Next
    
            bmp.UnlockBits(bitmapData)
    
            'get and fillup a grayscale-palette
            Dim pal As System.Drawing.Imaging.ColorPalette = bmp.Palette
    
            For i As Integer = 0 To 255
                pal.Entries(i) = Color.FromArgb(255, i, i, i)
            Next
    
            bmp.Palette = pal
    
            Dim bOld As Image = Me.pictureBox1.Image
            Me.pictureBox1.Image = bmp
    
            If bOld IsNot Nothing Then
                bOld.Dispose()
            End If
    
            MessageBox.Show(bmp.PixelFormat.ToString())
        End Sub
    
        'sets a different palette
        Private Sub button2_Click(sender As Object, e As EventArgs)
            If Me.pictureBox1.Image IsNot Nothing Then
                Dim bmp As Bitmap = DirectCast(Me.pictureBox1.Image, Bitmap)
    
                If bmp.PixelFormat.Equals(System.Drawing.Imaging.PixelFormat.Format8bppIndexed) Then
                    Dim pal As System.Drawing.Imaging.ColorPalette = bmp.Palette
    
                    'write a custom palette
                    For i As Integer = 0 To 255
                        pal.Entries(i) = Color.FromArgb(255, i \ 2, i, 127)
                    Next
    
                    bmp.Palette = pal
    
                    Me.pictureBox1.Refresh()
                End If
            End If
    
            'MessageBox.Show(Me.pictureBox1.Image.PixelFormat.ToString())
        End Sub
    End Class
    

    Regards,

      Thorsten

    Saturday, August 12, 2017 8:15 AM
  • You can ask it to the people who sell it as a product for instance these. 

    http://www.colorizephoto.com/how-to-colorize-black-and-white-photos

    Be aware that Adobe spent many years to create this kind of programs, so don't think you get this free in a forum. 

    But it can be done with a lot of sweat, blood and tears. 


    Success
    Cor

    Saturday, August 12, 2017 10:18 AM