Member Avatar for nssltd

Hey

During my web browser program i want to embed the very basic feature set as desktop background. i know how to set the image as desktop background but i am unsure how to get the image of the website

Any help is much appreciated

NSSLTD

Method to set image.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Runtime.InteropServices;

namespace DesktopWallpaper
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
            ddlStyle.SelectedIndex = 0;
            
            picThumbnail.SizeMode = PictureBoxSizeMode.Zoom;
            
            picThumbnail.ImageLocation = GetCurrentWallpaper();
        }

        private void btnSet_Click(object sender, EventArgs e)
        {
            if (openGraphic.ShowDialog() == DialogResult.OK)
            {
                
                picThumbnail.ImageLocation = openGraphic.FileName;
               
                picThumbnail.SizeMode = PictureBoxSizeMode.Zoom;
                
                SetWallpaper(openGraphic.FileName, 2, 0);
            }
        }

        private void SetWallpaper(string WallpaperLocation, int WallpaperStyle, int TileWallpaper)
        {
            
            SystemParametersInfo(20, 0, WallpaperLocation, 0x01 | 0x02);
            
            RegistryKey rkWallPaper = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
           
            rkWallPaper.SetValue("WallpaperStyle", WallpaperStyle);
            
            rkWallPaper.SetValue("TileWallpaper", TileWallpaper);
            rkWallPaper.Close();
        }

        private string GetCurrentWallpaper()
        {
           
            RegistryKey rkWallPaper = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", false);
            string WallpaperPath = rkWallPaper.GetValue("WallPaper").ToString();
            rkWallPaper.Close();
          
            return WallpaperPath;
        }
    }
}

Are you trying to save an image from a webpage, like Internet Explorer or firefox does?

This is untested and incomplete, But this should easily set you on your way to getting this to work

// somewhere attach the event handler
       webBrowser1.Document.MouseDown += new HtmlElementEventHandler(doc_MouseUp);
        

        //handle the event
        void doc_MouseDown(object sender, HtmlElementEventArgs e)
        {
            //used middle button, could be any
            if (e.MouseButtonsPressed == MouseButtons.Middle)
            {
                var doc = sender as HtmlDocument;
                HtmlElement imagetag = doc.GetElementFromPoint(e.MousePosition);
                string tag = imagetag.InnerHtml;

                //use regex to parse image location then save it to the local path.
            }
        }

Hope this sets you on your way. The HTMLDocument class that represents the actual html that the browser is rendering is exposed by the document property, It has mouse events and the ever handy GetElementFromPoint method that lets you find out what your mouse is over.

Member Avatar for nssltd

Ok thanks for the response ill give it a go

Member Avatar for nssltd

Excuse me, but is this a mistake?

webBrowser1.Document.MouseDown += new HtmlElementEventHandler(doc_MouseUp);
void doc_MouseDown(object sender, HtmlElementEventArgs e)

one is mouse up and one is mouse down. which one is right or does this matter?

Member Avatar for nssltd

Sorry but im just not sure how to work this please can you help me more with how the code works and how i can develop it

Any help much appreciated

NSSLTD

It should be mouse down, I'm not sure what happens there but I was just typing code, I didn't test it. (although minus that error it should work just fine)

This code allows you to get the tag at a mouse point. you could handle the mouse up and use a custom context menu. or whatever you wanted. It should be pretty straightforward to use it for your purpose.

if you have any specific questions give me a post back.

Member Avatar for nssltd

Something i never covered when i was learning C# was how to save a picture like this... This is what i am stuck on.

I have the tag. But i need to save that image inside the HTML to a path which i have located in regedit. but i am unsure how to retrieve the image from inside the html and save it to a path of my choice. Please may you help me further

NSSLTD

Doesn't the webbrowser control already allow you to right click an image and select "set as background" ?

why re-implement it?

Also, from the code I gave you. using the webbrowser control's documentCompleted event to add the handler for the mouseup/mousedown event.

then get the element at the mouse location, and check if its tag is "IMG" if it is use its getAttribute method for the "src" and set that to a picturebox's imagelocation property. or use the webbrowser.download. if its not an IMG tag you can loop through that tag's children tags.

But I still don't recommend any of this vs. the webbrowser controls built in "set as background" context menu item.

Member Avatar for nssltd

Yes i know that but i want to rebuild that menu because Internet Explorers Menu has things like, Translate with bing; Email with Windows live; Blog with windows live; Export to excel.

It uses software i do not need and software my users do not necessarily need I want to rebuild the menu in a less microsoft controlled way.

well I don't have time to work out a solid solution for you. I assume you intend to replace the context menu with your own. I think that the honest best solution for this would be to get a hold of the creation of the context menu and filter out the objects you don't want. But that's advanced and requires a bit of unmanaged code. Similar to the process of adding context menu items to explorer items.

Sorry I can't be more help.

Member Avatar for nssltd

Its okay i guess ill go look at firefox's source code and find out how they do it, as far as i know its written in C++.

Member Avatar for nssltd

well I don't have time to work out a solid solution for you. I assume you intend to replace the context menu with your own. I think that the honest best solution for this would be to get a hold of the creation of the context menu and filter out the objects you don't want. But that's advanced and requires a bit of unmanaged code. Similar to the process of adding context menu items to explorer items.

Sorry I can't be more help.

Sorry i forgot to mention thanks for all your help!

how you get find what you are looking for.

Dare I say that anything you find in firefox's source code would be a far fetch to use with the native .net webbrowser control. But if you find anything useful. Please do post it for the community to learn from.

Member Avatar for nssltd

Yes i understand where you are coming from but aswell as that i thought I'd look at the workings of a Custom Made Webbrowser and see if i could develop one myself. I do know it will be allot of work and i do know what i need to incorporate but it something to do. In addition to that i will post here anything i do find if i believe it is useful.

NSSLTD

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.