BMP String aus PictureBox erstellen

22/02/2009 - 12:47 von Wolfgang Bußmann | Report spam
Hallo an alle 'Grafikexperten',

mit dem Befehl SavePicture Picture1.Image, ("C:\test.bmp") kann ich aus
einer PictureBox eine BMP Datei erzeugen.

Diese PictureBox kann ich bei picture1.Autoredraw = true vorher
entsprechend bearbeiten. Z. B. Text oder Linien einfügen.

Zur Ansteuerung einer LED-Anzeige muss ich nun die abgespeicherte
BMP-Datei als String einlesen und über den ComPort versenden. Dies
funktioniert mit folgendem Code auch einwandfrei.

Groesse = FileLen(Dateiname)
RT = String$(Groesse, " ")

Filenr = FreeFile
Open Dateiname For Binary As #Filenr
Get #Filenr, 1, RT
Close #Filenr

... String über Comport versehen

Nun muss ich nach jeder Änderung der PictureBox erst eine Datei
erzeugen, neu in den String einlesen und dann ausgeben.

Gibt es evtl. eine einfachere Möglichkeit den erforderlichen String zu
erzeugen?

Eine alternative Möglichkeit wàre evtl. noch in eine "virtuelle" Datei,
die nur im Speicher existiert, zu schreiben. Gibt es das überhaupt?

Ich wàre für einen Denkanstoß dankbar.

Gruß Wolfgang
 

Lesen sie die antworten

#1 Dieter Strassner
22/02/2009 - 13:10 | Warnen spam
Hallo Wolfgang,

Gibt es evtl. eine einfachere Möglichkeit den erforderlichen String zu
erzeugen?



Kopiere u.g.Code in ein BAS-Modul. Orginal siehe Quellenangabe:
____________________________________________

' **********************************************
' ** **
' ** **
' ** ByteArray einer Grafik zum **
' ** laden einer Picturebox/ImageFeld **
' ** **
' ** **
' **********************************************


Option Explicit
'
' Copyright © 1997-1999 Brad Martinez, http://www.mvps.org
'
' Quelle: http://www.mvps.org/btmtz/_misc/index.htm
'
' Load GIFs and JPGs from file, as well as all other supported
' graphics formats (bmp, dib, wmf,.emf, ico, cur).
' Is based loosely on the C code from the following:
' "Q218972 - SAMPLE: How To Load and Display Graphics Files w/LOADPIC.EXE"
' http://support.microsoft.com/suppor...8/9/72.ASP

Public Enum CBoolean ' enum members are Long data types
CFalse = 0
CTrue = 1
End Enum

Public Const S_OK = 0 ' indicates successful HRESULT

'WINOLEAPI CreateStreamOnHGlobal(
' HGLOBAL hGlobal, // Memory handle for the stream object
' BOOL fDeleteOnRelease, // Whether to free memory when the object is
released
' LPSTREAM * ppstm // Indirect pointer to the new stream object
');
Declare Function CreateStreamOnHGlobal Lib "ole32" _
(ByVal hGlobal As Long, _
ByVal fDeleteOnRelease As CBoolean, _
ppstm As Any) As Long

'STDAPI OleLoadPicture(
' IStream * pStream, // Pointer to the stream that contains picture's
data
' LONG lSize, // Number of bytes read from the stream
' BOOL fRunmode, // The opposite of the initial value of the picture's
property
' REFIID riid, // Reference to the identifier of the
interface describing the type
' // of interface pointer to return
' VOID ppvObj // Indirect pointer to the object, not AddRef'd!!
');
Declare Function OleLoadPicture Lib "olepro32" _
(pStream As Any, _
ByVal lSize As Long, _
ByVal fRunmode As CBoolean, _
riid As GUID, _
ppvObj As Any) As Long

Private Type GUID ' 16 bytes (128 bits)
dwData1 As Long ' 4 bytes
wData2 As Integer ' 2 bytes
wData3 As Integer ' 2 bytes
abData4(7) As Byte ' 8 bytes, zero based
End Type

Declare Function CLSIDFromString Lib "ole32" (ByVal lpsz As Any, pclsid As
GUID) As Long

Private Const sIID_IPicture = "{7BF80980-BF32-101A-8BBB-00AA00300CAB}"

Private Const GMEM_MOVEABLE = &H2
Declare Function GlobalAlloc Lib "kernel32" (ByVal uFlags As Long, ByVal
dwBytes As Long) As Long
Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long

Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any,
pSource As Any, ByVal dwLength As Long)
'

Public Function PictureFromBits(abPic() As Byte) As IPicture ' not a
StdPicture!!

Dim nLow As Long
Dim cbMem As Long
Dim hMem As Long
Dim lpMem As Long
Dim IID_IPicture As GUID
Dim istm As stdole.IUnknown ' IStream
Dim ipic As IPicture

' Get the size of the picture's bits
On Error GoTo Out
nLow = LBound(abPic)
On Error GoTo 0
cbMem = (UBound(abPic) - nLow) + 1

' Allocate a global memory object
hMem = GlobalAlloc(GMEM_MOVEABLE, cbMem)
If hMem Then

' Lock the memory object and get a pointer to it.
lpMem = GlobalLock(hMem)
If lpMem Then

' Copy the picture bits to the memory pointer and unlock the handle.
MoveMemory ByVal lpMem, abPic(nLow), cbMem
Call GlobalUnlock(hMem)

' Create an ISteam from the pictures bits (we can explicitly free hMem
' below, but we'll have the call do it...)
If (CreateStreamOnHGlobal(hMem, CTrue, istm) = S_OK) Then
If (CLSIDFromString(StrPtr(sIID_IPicture), IID_IPicture) = S_OK)
Then

' Create an IPicture from the IStream (the docs say the call does
not
' AddRef its last param, but it looks like the reference counts
are correct..)
Call OleLoadPicture(ByVal ObjPtr(istm), cbMem, CFalse,
IID_IPicture, PictureFromBits)

End If ' CLSIDFromString
End If ' CreateStreamOnHGlobal
End If ' lpMem

' Call GlobalFree(hMem)
End If ' hMem

Out:
End Function


Viele Grüße

Dieter


Rückfragen bitte nur in die Newsgroup!

EDV-Kommunikation Strassner e.K.
68623 Lampertheim
Internet: www.strassner.biz

Ähnliche fragen