?
Solved

Create image file from Picturebox graphics

Posted on 2003-03-21
4
Medium Priority
?
224 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
[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
  • 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

Industry Leaders: 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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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
Course of the Month13 days, left to enroll

777 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