Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Auto Cropping 3

Status
Not open for further replies.

TariqMehmod

Programmer
Mar 4, 2004
100
PK
ear Experts

I have following excel file

aaa_luwzwu.png


I want to create an image based on usedcells (B3:J22)
Is it possible to crop required range then create an image.

I think GDIPLUS can do this, so I found these links but still confuse how to do this.


The codes in this link crop whole screen but not required area

Please help
 
The two links you posted to the VfpImaging blog give you the information you need. You must first do a screen capture to save the image as a BMP (this is described in the FAQ, under the heading "How can I capture a VFP screen and save it?"). That will give you a BMP (or any other supported file format if you change .SaveAsBMP as appropriate). You can then use the code shown under "Crop Images with GDI Plus" to crop to the required area.

Note that the screen capture code requires a Windows handle. This would have to be a handle to the Excel window, not VFP.

Now having said all that, I can't help thinking there is an easier solution. Rather than capturing the relevant portion of the worksheet as an image, why not create it within VFP as a report? I assume you already have the data in a VFP table or cursor (if you don't, you can easily import it from the worksheet). It should then be an easy matter to create a report with all the fonts, colours, borders, etc. exactly as you have shown in the worksheet.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Sir I want send image file on whatsapp so vfp report will not do the work.
 
If you wrote the report to a pdf you could send that on whatsapp couldn't you?

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
Respected sir,

the data is on the fourth portion of the page, the rest area appears blank.
If I create image file then there will be no blank space.
For this specific reason I want to create image file.


Finally I found working codes
Code:
 lcXLS = Getfile('XLSx')
Set Textmerge To Memvar myVar Noshow
Set Textmerge On
\DEFINE CLASS oleExcelObject as OLEControl
\ OleClass ="Excel.Sheet"  && Server name
\ OleTypeAllowed = 0      && Linked
\ DocumentFile = "<<m.lcXLS>>" && This file should exist
\ENDDEFINE
Set Textmerge To
Set Textmerge Off
lcTempClass = Sys(2015)+'.prg'
Strtofile(myVar, lcTempClass)
Compile (lcTempClass)

Public oForm
oForm = Createobject("Form")
oForm.Show()

With oForm
	
	.Height = 460
	.Width = 360

	.Newobject("ExcelObject","oleExcelObject",lcTempClass)  && Add OLE object
	With .ExcelObject
		.Left = 5
		.Top = 5
		.Width = .Parent.Width - 10
		.Height = .Parent.Height - 10
		.Visible = .T.
	ENDWITH

ENDWITH



oForm.Show
Read Events
Clear Class 'oleExcelObject'
Erase (Juststem(m.lcTempClass)+'.*')]

Now want to add a button on above form
with these codes

Code:
 DEFINE CLASS cmdMyCmdBtn AS CommandButton
   Caption = '\<Quit'
   Cancel = .T.
   Left = 125
   Top = 210
   Height = 25
   visible = .t.
   PROCEDURE Click
      CLEAR EVENTS
ENDDEFINE]

Where to add below codes in above codes

Please
 
OK, we'll accept that you have to have an image file. But you still don't need to do a screen capture from Excel. You can create a report of the data in VFP, as suggested above; then "print" the report as an image.

The easiest way to do that would be to install FoxyPreviewer. That will let you save a report as an image with just a few lines of code. It supports BMP, TIFF, JPEG and other common formats.

Another option would be to install the Microsoft Office Document Image Writer print driver, which lets you "print" to TIFF. In that case, you won't need any special code apart from the usual SET PRINTER and REPORT FORM commands.

I think either of these options would be easier for you than trying to use the VFPImaging product.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Tariq, your latest post crossed with mine.

As far as I can see, the only thing that your code does is to open a VFP form and display the worksheet within it. That's a good first step. But there is still no direct way of converting that to an image. You would still have to use the VfpImaging control to do that. The only benefit compared to the sample code that is shown in their blog is that you won't have to worry about getting a handle to the Excel window. You can use the form's own HWnd property for that.

But before you go down that route, please read my previous post, which I think you will find easier to handle.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Sir I am already working with Foxypreviewer but the problem is Full page.
Foxpriviewer will create a report on fill page and rest of the portion will be with white space.

I have special odes wtith GDIPLUS to convert VFP FORM's controls into image file.
If I get success to add command button in my above codes then I shall put those codes under clink.
As a result the form contents will goes to an image file.

I want to add these codes in my above post

Code:
 ADD OBJECT COMMAND1 AS COMMANDBUTTON WITH ;
      top=240,;
      left=20,;
      width=80,;
      height=24,;
      caption="Modify",;
      name="command1"
      		
PROCEDURE COMMAND1.CLICK
THISFORM.OLECONTROL1.DOVERB(0)
ENDPROC]

or like these

Code:
 DEFINE CLASS cmdMyCmdBtn AS CommandButton
   Caption = '\<Quit'
   Cancel = .T.
   Left = 125
   Top = 210
   Height = 25
   visible = .t.
   PROCEDURE Click
      CLEAR EVENTS
ENDDEFINE]]

Regards
 
I thought it was possible to specify a page size for any report...

Horse to water... make it drink?

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
Sir the image consists dashboard data. User will not be involve to do anything. he will just click one buttin then as a result following actions will be perfomed.

1-create dashborad data in temporary file
2-Cursor data will create excel file
3-Excel fill will be loaded to VFP form.
4-An image will be created
5-That image will go to whatsapp

That above all will be on one click

I have done 3 steps out of 5

Now facing problem to add a button to run codes that will convert image on form to an image file. I have already those codes.

Please help me to add a commandbutton on defined form in my previous post.

after that all will be done successfuly.

Regards

 
The code shown in will deal with step 4. You can use the code exactly as it is, the only change you might need being the name of the image file in the 8th line.

Don't get too hung up on the command button. That's just a place for you to execute your code. The problem with putting a command button on the form is that the button itself will also appear in the image, which is clearly not what you want. Better to put the code in a menu bar, toolbar button or another form. But in that case, you will also have to change THISFORM (in line 7) to a reference to the actual form.

Alternatively, put the button on the form, and place the code in the button, but add a line to make the button invisible before you do the capture, and another to make it visible again afterwards.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Mike

Yes, the first part of the button click event should be to hide the button?
Code:
this.visible=.f.

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
Sir Mike
You wrtoe

Don't get too hung up on the command button. That's just a place for you to execute your code. The problem with putting a command button on the form is that the button itself will also appear in the image, which is clearly not what you want. Better to put the code in a menu bar, toolbar button or another form. But in that case, you will also have to change THISFORM (in line 7) to a reference to the actual form.]

THISFORM (in line 7)

Where is this line in my codes?
May be I am wrong.

Sir if you do not want to add a commandbutton on form then no matter.
We can directly add codes on the click event of form.
But the problem is how to call Click event of my defined form.

Please

I tried in this way

cc_ox0ofg.png


but still failed.
 
I think CommandButton needs to be in quotes for your example

Code:
.Newobject("cmdMyCmdBtn","CommandButton",lcTempClass)

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
The reason for the error is that it is expecting the second parameter to .NewObject to be a variable, named CommandButton. If CommandButton is the actual name of the class, rather than a variable which holds the name of the class, you need to put in quotes: [highlight #EDD400]"[/highlight]CommandButton[highlight #EDD400]"[/highlight].

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Griff beat me to it.

But, Tariq, I have to say that this is getting to the stage where you are using the thread just as a way of debugging your code. We are happy to help with that, of course, but you can do yourself a favour. Your whole approach appears to be one of searching on line for some existing code that might or might not solve your problem, then coming here for help when it doesn't work as expected.

There's nothing wrong with using code that you find on-line (copyright restriction permitting), but you would do much better if you study the code and understand what it is doing, rather than blindly copying and pasting it into your application. That way, you will improve your overall knowledge and become a better developer. That doesn't mean that you should not ask for help here, but you will learn more if you asked focused question rather than simply expecting other people to debug your code.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Respected Both Professor

Finally I used these codes

Code:
 *lcXLS = Getfile('XLSx')

lcXLS =[C:\xls\Daily.xlsx]
Set Textmerge To Memvar myVar Noshow
Set Textmerge On
\DEFINE CLASS oleExcelObject as OLEControl
\ OleClass ="Excel.Sheet"  && Server name
\ OleTypeAllowed = 0      && Linked
\ DocumentFile = "<<m.lcXLS>>" && This file should exist
\ENDDEFINE


\DEFINE CLASS cmdMyCmdBtn AS CommandButton
\ Caption = '\<Quit'
\ Cancel = .T.
\ Left = 125
\ Top = 520
\ Height = 25
\ visible = .t.
\ PROCEDURE Click
\ this.visible=.f.
\ #INCLUDE gpImage.h
\ IF Not "gpImage" $ SET("Procedure")
\ SET PROCEDURE TO gpImage ADDITIVE
\ ENDIF
\ gdip = CREATEOBJECT("gpInit")
\ img = CREATEOBJECT("gpImage")
\ img.Capture(Thisform.HWnd)
\ LOCAL lnTitleHeight, lnLeftBorder, lnTopBorder
\ lnTitleHeight = SYSMETRIC(9)
\ lnLeftBorder = SYSMETRIC(3)
\ lnTopBorder = SYSMETRIC(4)
\ Img.Crop(lnLeftBorder, lnTitleHeight + lnTopBorder, ;
\ Img.ImageWidth - (lnLeftBorder * 2), ;
\ Img.ImageHeight - (lnTitleHeight + (lnTopBorder * 2)))
\ Img.SaveasBMP("Captured_Clean")
\ Img = NULL
\ Gdip = NULL
\ENDDEFINE


Set Textmerge To
Set Textmerge Off
lcTempClass = Sys(2015)+'.prg'
Strtofile(myVar, lcTempClass)
Compile (lcTempClass)

Public oForm
oForm = Createobject("Form")

With oForm

	.Height = 550
	.Width = 360

	.Newobject("ExcelObject","oleExcelObject",lcTempClass)  && Add OLE object
	With .ExcelObject
		.Left = 5
		.Top = 5
		.Width = .Parent.Width - 10
		.Height = .Parent.Height - 10
		.Visible = .T.
	Endwith

	.Newobject("commandbutton","cmdMyCmdBtn",lcTempClass)  && Add OLE object

Endwith

oForm.Show
Read Events
Clear Class 'oleExcelObject'
Erase (Juststem(m.lcTempClass)+'.*')]

as a result I got this image file

cc_drsol2.png


All credit goes to you

But new issue has raised.

Everytime when you use above codes a new files is created in same folder like this

dd_kmnegr.png


How to get rid of this situation?

I think this last line is not working

Code:
 Erase (Juststem(m.lcTempClass)+'.*')]

Please
 
Try
Code:
Erase (Juststem(lcTempClass)+'*.*')

But, be warned if lcTempClass doesn't have something in it (like c:\temp\_5mw15)
you run the risk of deleting all your own code or emptying whatever the current folder has in it!


Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 

Sir still no difference.

Files are being created every time.

I think this command

Code:
Erase (Juststem(lcTempClass)+'*.*')]

will not work until we will free/release them from memory.






 
Why do you keep putting a right hand square bracket on the end of the command?

Try this as it seems to be consistent

Code:
Erase ('_5WM*.*')

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top