Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Create image file from Picturebox graphics

Posted on 2003-03-21
4
Medium Priority
?
237 Views
Last Modified: 2010-04-07
I have created images in a Picturebox using the Line method. How can I create a graphics file (eg jpg) using this image?
0
Comment
Question by:PeterN
  • 2
4 Comments
 
LVL 5

Expert Comment

by:Cimperiali
ID: 8182752
This code comes from
http://www.codeguru.com/forum/showthread.php?s=&threadid=20900
And the author is a smart one - unluckily, I am not that one....;-)

Re: PictureBox control
If you don't intended to know how I get this done. just copy & 
paste the code to get the result. But at least, you should beware
two things :

1. vbFileIO.ReadFileStr is just a function to read the file into a
string variable, but in you case, you don't need this as you
already got in directly from the internet (I assume).

2. If you want to exit the program, call GlobalFree API to free the
hGlobal. if not, you might get memory leak.

Okay, if you intended to know how it work, here it is :

Generally the picture box .Picture is IPicture interface of COM. the
LoadPicture function actually create a IPicture and set the .Picture
to the newly created .Picture. It always assume that the data is
in the file. Because you already got the data, so you need to do
to it manual, i.e. emulate LoadPicture which take in String
variable rather than File.

The first step is to allocate the memory and lock it in heap as,
first, the string variable is local and will go off as soon as you
leave the load procedure, second the string variable might be
UNICODE, that's why the following loop convert the string
variable to byte array.

After converting to byte array, we then move the data into the
global heap that we just allocate using RtlMoveMemory.


okay, we already setup the stream data properly, next, we are
going to use the COM function OleLoadPicture to create a new
IPicture. But before that, because OleLoadPicture need to have
IStream as it first parameter. Thus, we use the COM function
CreateStreamOnHGlobal to create a pointer of IStream.


right after this, we can use OleLoadPicture to create a IPicture
object using the IStream that we just created. the iID_IPicture is
the CSLID of the IPicture Interface.

after OleLoadPicture, we get a newly created IPicture, then we
can se the Picture1.Picture to the newly created IPicture.

did you see that the IPic variable give you the size of the picture
as well ?



code:------------------------------------
Private Declare Sub RtlMoveMemory Lib "kernel32" (Des as Any, Src as Any, byval Length as Long)
private Declare Function GlobalAlloc Lib "kernel32" (byval wFlags as Long, byval dwBytes as Long) as Long
private Declare Function GlobalFree Lib "kernel32" (byval hMem as Long) as Long
private Declare Function GlobalLock Lib "kernel32" (byval hMem as Long) as Long
private Declare Function OleLoadPicture Lib "olepro32.dll" (byval pStream as Long, byval lSize as Long, byval fRunmode as Long, RefIID as fGUID, iPic as IPicture) as Long
private Declare Function CreateStreamOnHGlobal Lib "ole32.dll" (byval hGlobal as Long, byval fDeleteOnRelease as Long, ppStm as Long) as Long


private Type fGUID
  Data1 as Long
  Data2 as Integer
  Data3 as Integer
  Data4(7) as Byte
End Type

Dim vbFileIO as new vbFileIO

private Sub Form_Load()
Dim iID_IPicture as fGUID
Dim iPic as IPicture 'IPicture requires a reference to "Standard OLE Types"
Dim IStream as Long
Dim streamstr as string
Dim hGlobal as Long
Dim mPtr as Long
Dim streamb() as Byte
Dim i as Long

  vbFileIO.ReadFileStr "d:\windows\system\oobe\images\bgdiscov.jpg", streamstr
  Size = len(streamstr)
  ReDim streamb(Size - 1)
   
  hGlobal = GlobalAlloc(2&, Size)
  mPtr = GlobalLock(hGlobal)
   
  for i = 1 to len(streamstr)
    streamb(i - 1) = Asc(mid(streamstr, i, 1))
  next i

  RtlMoveMemory byval mPtr, byval VarPtr(streamb(0)), Size
   
  CreateStreamOnHGlobal hGlobal, 0&, IStream
   
  With iID_IPicture
    .Data1 = &H7BF80980
    .Data2 = &HBF32
    .Data3 = &H101A
    .Data4(0) = &H8B
    .Data4(1) = &HBB
    .Data4(3) = &HAA
    .Data4(5) = &H30
    .Data4(6) = &HC
    .Data4(7) = &HAB
  End With
   
  'Create Picture object
  result = OleLoadPicture(IStream, Size, 0, iID_IPicture, iPic)
     
  'Return the new Picture object
  set Picture1.Picture = iPic


End Sub
-----------------------

HTH

cksiow
http://vblib.virtualave.net - share our codes

0
 
LVL 5

Expert Comment

by:Cimperiali
ID: 8182783
0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 2000 total points
ID: 8182789
or if you don't mind saving as BMP:

Picture1.ScaleMode = vbPixels   ' Set scale to pixels.
Picture1.AutoRedraw = True ' Turn on AutoRedraw.

Picture1.Line (0, 0)-(300, 300)
SavePicture Picture1.Image, "C:\my documents\test.bmp"
0
 

Author Comment

by:PeterN
ID: 8189864
Thanks. Such a simple solution! I would like to convert the .bmp to .jpg to reduce space requirements. I don't know if this is possible???

Cimperiali - thanks for your answer but bobbit31's solution was much easier. Unfortunately, I was unable to open the 2 links you provided. I appreciate your effort.
Regards to both of you
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…
Suggested Courses
Course of the Month10 days, 3 hours left to enroll

569 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