Computing.Net > Forums > Programming > Center GIF in WebBrowser

Center GIF in WebBrowser

Reply to Message Icon

Original Message
Name: Sci-Guy
Date: January 16, 2005 at 19:53:19 Pacific
Subject: Center GIF in WebBrowser
OS: XP Pro SP1
CPU/Ram: Athlon XP 2600+ / 1024Mb
Comment:

I have a program that I wrote in VB6. It uses the WebBrowser control to display animated GIF's. As most of you are probably aware, when a picture is opened in a web browser it is aligned to the left. I would like the GIF to be centered horizontally in the browser if at all possible. Does anyone know if I can achieve this with code?


Please let us know if you found someone's advice to be helpful.


Report Offensive Message For Removal


Response Number 1
Name: Michael J (by mjdamato)
Date: January 16, 2005 at 21:55:12 Pacific
Reply: (edit)

Yes, put it in a table with 100% width and center it in a single sell such ass (<> replaced with []).

[table width=100%]
[tr]
[td align=center]
[img src=image.gif]
[/td]
[/tr]
[/table]

You could also center it vertically by giving the table 100% height.

Michael J


Report Offensive Follow Up For Removal

Response Number 2
Name: Sci-Guy
Date: January 17, 2005 at 01:53:08 Pacific
Reply: (edit)

Thanks for the quick reply, but I've tried that without success. The problem is that the GIF to be displayed is triggered by clicking one of an array of buttons. It's further complicated by the fact that each button in the array can be configured to display any GIF or JPEG that the user has in the default folder. Therefore, the path for the image source cannot be defined as: [img src=image.gif].

Here is the existing code for the button array.
---
Private Sub cmdUser_Click(Index As Integer)

For intCtr = (Forms.Count - 1) To 1 Step -1
Unload Forms(intCtr)
Next intCtr
frmWeb.Show
frmWeb.WebBrowser1.Navigate strPic + strFile(Index)
frmMain.SetFocus

End Sub
---
This code works well except for the fact that the images are aligned left.


Please let us know if you found someone's advice to be helpful.


Report Offensive Follow Up For Removal

Response Number 3
Name: Sci-Guy
Date: January 17, 2005 at 04:18:40 Pacific
Reply: (edit)

Here's a couple things I've tried.
Note: I've used ] imstead of > for posting purposes only.
-------
Private Sub cmdUser_Click(Index As Integer)

For intCtr = (Forms.Count - 1) To 1 Step -1
Unload Forms(intCtr)
Next intCtr
frmWeb.Show
strhtml = "about:[html][body bgcolor=white scroll='no']"
strhtml = strhtml & "[table border=0 width=100%][tr][td align=center]"
strhtml = strhtml + "[img src=strPic + strFile(Index)][/img]"
strhtml = strhtml & "[/td][/tr][/table][/body][/html]"
frmWeb.WebBrowser1.Navigate strhtml
frmMain.SetFocus

End Sub
-------------------------
That produces a box with a red "X" centered in the browser.
-------------------------
Private Sub cmdUser_Click(Index As Integer)

For intCtr = (Forms.Count - 1) To 1 Step -1
Unload Forms(intCtr)
Next intCtr
frmWeb.Show
strhtml = "about:[html][body bgcolor=white scroll='no']"
strhtml = strhtml & "[table border=0 width=100%][tr][td align=center]"
strhtml = strhtml + strPic + strFile(Index)
strhtml = strhtml & "[/td][/tr][/table][/body][/html]"
frmWeb.WebBrowser1.Navigate strhtml
frmMain.SetFocus

End Sub
-------------------------
That produces the full path (as a string) centered in the browser.

No matter how I change the above code, I can't get the image to display.


Please let us know if you found someone's advice to be helpful.


Report Offensive Follow Up For Removal

Response Number 4
Name: Michael J (by mjdamato)
Date: January 17, 2005 at 20:57:12 Pacific
Reply: (edit)

Not too familiar with VB6, but it makes sense that the above code only prints the path instead of the actual image because you did not create the image tag.

The example you posted in Reply 2 displayed the image because you were directing the browser to open an image - not an html page. In that manner, IE is functioning as an image viewer not a web browser. There is NO way to change the alignment of an image when opened in that manner.

The code you posted in Response 3 will work if you include the path in an image tag by adding the two lines identified below:

-------------------------
Private Sub cmdUser_Click(Index As Integer)

For intCtr = (Forms.Count - 1) To 1 Step -1
Unload Forms(intCtr)
Next intCtr
frmWeb.Show
strhtml = "about:[html][body bgcolor=white scroll='no']"
strhtml = strhtml & "[table border=0 width=100%][tr][td align=center]"
strhtml = strhtml & "[img src="
strhtml = strhtml + strPic + strFile(Index)
strhtml = strhtml & "]"
strhtml = strhtml & "[/td][/tr][/table][/body][/html]"
frmWeb.WebBrowser1.Navigate strhtml
frmMain.SetFocus

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

Hope this helps.

Michael J


Report Offensive Follow Up For Removal

Response Number 5
Name: Sci-Guy
Date: January 17, 2005 at 22:02:34 Pacific
Reply: (edit)

Michael,
Thank you. The code you posted has solved this for me. It wasn't important, but annoying. I guess annoying things drive me a little crazy. Thanks again.


Please let us know if you found someone's advice to be helpful.


Report Offensive Follow Up For Removal


Response Number 6
Name: Sci-Guy
Date: January 21, 2005 at 19:58:32 Pacific
Reply: (edit)

It seems I spoke too soon. The code runs well from the IDE and once compiled runs well from the folder in which the project is stored. However, if I use the PDW then install the program, all I get is a box with a red "X" centered in the browser. I also tried copying the exe file (along with needed files and folders) to another folder and get the same problem. This didn't happen before introducing the code to center the image. Any ideas as to what the problem might be?


Please let us know if you found someone's advice to be helpful.


Report Offensive Follow Up For Removal

Response Number 7
Name: Sci-Guy
Date: January 21, 2005 at 20:16:07 Pacific
Reply: (edit)

Correction: It just won't work from the "Program Files" folder on the C:\ drive.


Please let us know if you found someone's advice to be helpful.


Report Offensive Follow Up For Removal

Response Number 8
Name: Sci-Guy
Date: January 21, 2005 at 20:49:57 Pacific
Reply: (edit)

Another Correction: It seems to not work from any folder that has a space in it's path, e.g. "Program Files", "New Folder", "My Documents".


Please let us know if you found someone's advice to be helpful.


Report Offensive Follow Up For Removal

Response Number 9
Name: Sci-Guy
Date: January 22, 2005 at 02:43:02 Pacific
Reply: (edit)

Update: Fixed!


Please let us know if you found someone's advice to be helpful.


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Center GIF in WebBrowser

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 6 Days.
Discuss in The Lounge