Solved

how to convert a bmp to a gray scale bmp

Posted on 2003-11-27
3
1,017 Views
Last Modified: 2011-09-20
I need to convert a bmp to a gray scale bmp and show it using a picbox, how can I do that?

thanks in advance
0
Comment
Question by:paulmccartneyjr
3 Comments
 
LVL 14

Accepted Solution

by:
aelatik earned 500 total points
ID: 9832889
Put a picturebox on you form and load a picture in it. After that paste the following code and call the function from a button.

Option Explicit

Private Type BITMAP
    bmType As Long
    bmWidth As Long
    bmHeight As Long
    bmWidthBytes As Long
    bmPlanes As Integer
    bmBitsPixel As Integer
    bmBits As Long
End Type
Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
Private Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long

Private Sub MakeGreyScale()
    Dim PicBits() As Byte, PicInfo As BITMAP
    Dim Cnt As Long, BytesPerLine As Long
    Dim iGreyScale As Long
       
    GetObject Picture1.Image, Len(PicInfo), PicInfo
    BytesPerLine = (PicInfo.bmWidth * 3 + 3) And &HFFFFFFFC
    ReDim PicBits(1 To BytesPerLine * PicInfo.bmHeight * 3) As Byte

    GetBitmapBits Picture1.Image, UBound(PicBits), PicBits(1)

    For Cnt = 1 To UBound(PicBits) Step 4
        iGreyScale = (0.11 * PicBits(Cnt)) + (0.59 * PicBits(Cnt + 1)) + (0.3 * PicBits(Cnt + 2))
        PicBits(Cnt) = iGreyScale
        PicBits(Cnt + 1) = iGreyScale
        PicBits(Cnt + 2) = iGreyScale
    Next Cnt

    SetBitmapBits Picture1.Image, UBound(PicBits), PicBits(1)
    Picture1.Refresh
End Sub

Private Sub Command1_Click()
    MakeGreyScale
End Sub

Source : http://www.visualbasicforum.com/showthread.php?threadid=86500
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9833137
hearing...
0
 
LVL 27

Expert Comment

by:Ark
ID: 9970040
Hi
'Gray color according ITU standard:
lGray = (222# * rgb.rgbRed + 707# * rgb.rgbGreen + 71# * rgb.rgbBlue) / 1000
See http://www.freevbcode.com/ShowCode.Asp?ID=4997 for motre image processing
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel object stays open 19 76
VB6 Compile Compatibility Issue 4 102
Determine Range to Select 5 48
maro to copy and paste from one worksheet to another based on a condition 11 54
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

809 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