?
Solved

how to convert a bmp to a gray scale bmp

Posted on 2003-11-27
3
Medium Priority
?
1,036 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 14

Accepted Solution

by:
aelatik earned 1500 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 28

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses

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