Solved

how to convert a bmp to a gray scale bmp

Posted on 2003-11-27
3
1,007 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
Comment Utility
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
Comment Utility
hearing...
0
 
LVL 27

Expert Comment

by:Ark
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

762 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

6 Experts available now in Live!

Get 1:1 Help Now