Solved

how to convert a bmp to a gray scale bmp

Posted on 2003-11-27
3
1,014 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now