Buat yg pintar VB, tolong saya...
saya buat program sederhana utk mengolah gambar, tapi masalahnya gambar hasil olahan tdk bisa disimpan.... dibawah ini saya sertakan potongan listingnya, untuk menampilkan form menyimpan file:
Begin VB.Form frmDlgSimpan
BorderStyle = 4 'Fixed ToolWindow
Caption = "Simpan Gambar"
ClientHeight = 3765
ClientLeft = 45
ClientTop = 285
ClientWidth = 6315
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 251
ScaleMode = 3 'Pixel
ScaleWidth = 421
ShowInTaskbar = 0 'False
StartUpPosition = 2 'CenterScreen
Begin VB.FileListBox file
Height = 2235
Left = 3180
Pattern = "*.bmp"
TabIndex = 8
Top = 60
Width = 3015
End
Begin VB.DirListBox dir
Height = 1890
Left = 180
TabIndex = 7
Top = 420
Width = 2955
End
Begin VB.DriveListBox drive
Height = 315
Left = 180
TabIndex = 6
Top = 60
Width = 2955
End
Begin VB.CommandButton cmdCancel
Caption = "Batal"
Height = 375
Left = 4920
TabIndex = 5
Top = 3330
Width = 1215
End
Begin VB.CommandButton cmdSave
Caption = "Simpan"
Enabled = 0 'False
Height = 375
Left = 3600
TabIndex = 4
Top = 3330
Width = 1215
End
Begin VB.TextBox txtFile
Height = 285
Left = 900
TabIndex = 3
Top = 2820
Width = 4695
End
Begin VB.TextBox txtDir
Height = 285
Left = 900
Locked = -1 'True
TabIndex = 2
Top = 2460
Width = 5235
End
Begin VB.Label lbl
BackStyle = 0 'Transparent
Caption = "Nama File Yg Sama Akan Diganti (Overwrite)"
Height = 435
Index = 3
Left = 120
TabIndex = 10
Top = 3300
Width = 3135
End
Begin VB.Label lbl
AutoSize = -1 'True
BackStyle = 0 'Transparent
Caption = ".bmp"
Height = 195
Index = 2
Left = 5820
TabIndex = 9
Top = 2880
Width = 345
End
Begin VB.Line lin
BorderColor = &H00808080&
Index = 1
X1 = 8
X2 = 412
Y1 = 215
Y2 = 215
End
Begin VB.Line lin
BorderColor = &H00FFFFFF&
BorderStyle = 6 'Inside Solid
BorderWidth = 2
Index = 0
X1 = 8
X2 = 412
Y1 = 216
Y2 = 216
End
Begin VB.Label lbl
AutoSize = -1 'True
BackStyle = 0 'Transparent
Caption = "Nama File"
Height = 195
Index = 1
Left = 120
TabIndex = 1
Top = 2880
Width = 705
End
Begin VB.Label lbl
AutoSize = -1 'True
BackStyle = 0 'Transparent
Caption = "Direktori"
Height = 195
Index = 0
Left = 120
TabIndex = 0
Top = 2520
Width = 585
End
End
Attribute VB_Name = "frmDlgSimpan"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub cmdCancel_Click()
Unload Me
End Sub
Private Sub cmdSave_Click()
SavePicture frmMain.Picture1.Picture, txtDir.Text & txtFile.Text & ".bmp"
Unload Me
End Sub
Private Sub dir_Change()
file.Path = dir.Path
file.Refresh
txtDir.Text = dir.Path
If Right(dir.Path, 1) <> "\" Then txtDir.Text = dir.Path & "\"
End Sub
Private Sub drive_Change()
dir.Path = drive.drive
dir.Refresh
file.Path = dir.Path
file.Refresh
End Sub
Private Sub file_Click()
cmdSave.Enabled = True
txtFile.Text = Left(file.FileName, Len(file.FileName) - 4)
End Sub
Private Sub Form_Load()
dir.Path = currDir
dir.Refresh
file.Path = dir.Path
file.Refresh
End Sub
Private Sub txtFile_Change()
If Len(txtFile) <> 0 Then cmdSave.Enabled = True
If Len(txtFile) = 0 Then cmdSave.Enabled = False
End Sub
Gambar saya tampilkan di form utama (frmMain) dg nama picture1. Mohon bantuannya
Anda mau menyimpan gambar dalam format apa? Terkompresi atau tidak terkompresi? Kalau delphi atau pascal saya tidak begitu tahu, tapi anda pasti menggunakan API windows yang sudah disediakan, dan salah satunya mempunyai fungsi GDI.
Pertama, anda dapatkan nilai-nilai pixel yang ada di form anda dengan menggunakan GetPixel
Kutip
Coba anda lihat di WINAPI.hlp untuk melihat deskripsinya
The GetPixel function retrieves the red, green, blue (RGB) color value of the pixel at the specified coordinates.
COLORREF GetPixel(
HDC hdc, // handle of device context
int XPos, // x-coordinate of pixel
int nYPos // y-coordinate of pixel
);
Parameters
hdc
Identifies the device context.
nXPos
Specifies the logical x-coordinate of the pixel to be examined.
nYPos
Specifies the logical y-coordinate of the pixel to be examined.
Return Values
If the function succeeds, the return value is an RGB value. If the pixel is outside of the current clipping region, the return value is CLR_INVALID.
Remarks
The pixel must be within the boundaries of the current clipping region.
Not all devices support GetPixel. An application should call GetDeviceCaps to determine whether a specified device supports this function.
See Also
GetDeviceCaps, SetPixel
Kedua, sediakan memory dengan sebanyakan lebar kali panjang dikali 4 ((w*h)*4), jadi format gambar anda adalah format data mentah pixel yang sepanjang 4 bytes.
Ketiga, tuliskan memory yang anda sediakan tadi, bytes per bytes. Cuma begitu saja kok.
Kalau anda tidak tahu cara mendapatkan pixel, anda harus mempelajari WINAPI bernama GetDC untuk mendapatkan alamat hapalan layar anda, tempat untuk menampung pixel yang ditampilkan dilayar, setelah itu cukup alokasikan memory untuk disimpan, dan terakhir dapatkan semuanya sepanjang lebar dikali panjang kalikan 4 bytes, karena warna pada layar didefinisikan sebagai RGBA, dimana R-merah, G-hijau, B-biru.
Masih pusing? Tanyakan lagi lebih spesifik, biar saya pandu.
Arrrghh... pusing. Saya maunya disimpan dlm bitmap saja, yg sederhana. Saya sudah mengalokasikan memori kok, di program utama, buat mengolah kan perlu menyimpan data gambar dulu. apa perlu buat variabel lagi untuk menyimpan? Sayang sekali sekarang tdk bawa programnya, ni pakai komputer kampus. Pulang nanti saya lihat2 lagi.
Bikin loop
while (x=0; x<width;x++)
{
while (y=0; y<height; y++)
{
}
}
Kurang lebih seperti demikian. Setelah itu anda dapatkan nilai pixel tersebut satu persatu, misalkan
Pixel:=GetPixel(1,1);
(
Mungkin cara saya menjelaskan kurang bagus ya. Jadi intinya, anda harus tahu apaguna dari GetDC, kemudian guna dari GetPixel, kemudian bagaimana cara menampung data pada array(deretan) dua dimensi (yaitu x dan y), setelah itu menulis data ke harddisk. Cuma begitu saja kok.
Jadi begini langkahnya
1. Alokasikan memori dengan menggunakan GlobalAlloc ,GMEM_ZEROINIT or GMEM_FIXED,nSize
2. Dapatkan nilai setiap pixel yang panjangnya 4 bytes satu pixelnya disimpan ke penampung yang telah dialokasikan oleh langkah 1
3. Baru tulis data seperti yang anda inginkan.
Misalkan, 0-3 adalah panjang gambar, byte 4-7 adalah tingginya, byte 8-11 adalah kedalaman warnanya dsb dimana terakhir baru data yang ingin anda simpan.
OK,, mksh.mulai ada titik terang.saya butuh waktu agak lama buat mencerna ini, maklum bener2 pemula.