最近发现 http://cn.bing.com 主页每天都会有不同的背景图片,而且都蛮漂亮的。 于是有个想法,写个程序自动将bing站的背景
作为桌面背景。 考虑设置桌面背景这个‘专业’的工作我的本职java语言肯定是实现不了,临时学学C#'来实现。
其中包括,从http下载图片,调用DLL设置桌面背景等操作,一段一段的代码都是从网上搜索的。我只是把这些功能组合了起来。
代码估计会给专业写C#的人笑,但是,管它呢, 贴在这里做个纪念,也给自己以后写c#小程序,做个参考。
源码如下:
/*
* Created by SharpDevelop.
* User: hsieh
* Date: 2009/10/26
* Time: 21:23
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Web;
using System.Net;
namespace DesktopSwitch
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Download image from 'http://cn.bing.com'.");
Program dsw = new Program();
string tempImage = Path.GetTempPath() + "\\" + "bing.jpg";
dsw.GetImageFromBing(tempImage);
dsw.SetDestPicture(tempImage);
//Console.Write("Press any key to continue . . . ");
//Console.ReadKey(true);
}
private void GetImageFromBing(String imagePath)
{
string url = "http://cn.bing.com";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
Encoding encode = Encoding.GetEncoding("utf-8");
StreamReader sr = new StreamReader(stream, encode);
string html = null;
char[] readbuffer = new char[256];
int n = sr.Read(readbuffer, 0, 256);
while (n > 0)
{
string str = new string(readbuffer, 0, n);
html += str;
n = sr.Read(readbuffer, 0, 256);
}
StreamWriter streamw = File.CreateText(@"C:\test3.txt");
streamw.WriteLine(html);
streamw.Close();
//string pattern = "height: 267px; background-image: url\\(\\.+\\); opacity: 1;";
//url:'\/fd\/hpk2\/Hayden_ZH-CN1124177866.jpg',id:'bgDiv'
string pattern = @"url:'\\(/fd\\/[\w\d]+\\/[\w\d]+_ZH-CN[\d]{5,}\.jpg)',id:'bgDiv'";
Match match = Regex.Match(html, pattern);
if(match.Success)
{
string imageUrl = match.Groups[1].Value;
imageUrl = url + imageUrl.Replace("\\","");
System.Console.WriteLine("Set image '" + imageUrl + "' as the Desktop background.");
downloadImage(imageUrl,imagePath);
}
else
{
System.Console.WriteLine("Can't find image.");
System.Environment.Exit(-1);
}
}
private void downloadImage(String url, String imagePath)
{
try{
WebRequest request = WebRequest.Create(url);
request.ContentType = "image/jpeg";
Stream stream = request.GetResponse().GetResponseStream();
byte[] mbyte = new byte[100000];
int allmybyte = (int)mbyte.Length;
int startmbyte = 0;
while(allmybyte>0)
{
int m = stream.Read(mbyte,startmbyte,allmybyte);
if(m==0)
break;
startmbyte+=m;
allmybyte-=m;
}
FileStream fstr = new FileStream(imagePath,FileMode.OpenOrCreate,FileAccess.Write);
fstr.Write(mbyte,0,startmbyte);
stream.Close();
fstr.Close();
}catch(Exception e){
Console.WriteLine(e.Message);
Console.WriteLine("Get image from bing failed. Exit");
System.Environment.Exit(-1);
}
}
[DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
public static extern int SystemParametersInfo(
int uAction,
int uParam,
string lpvParam,
int fuWinIni
);
/// <summary>
/// 设置背景图片
/// </summary>
/// <param name="picture">图片路径</param>
private void SetDestPicture(string picture)
{
if (File.Exists(picture))
{
if (Path.GetExtension(picture).ToLower() != "bmp")
{
// 其它格式文件先转换为bmp再设置
string tempFile = @"C:\test.bmp";
Image image = Image.FromFile(picture);
image.Save(tempFile, System.Drawing.Imaging.ImageFormat.Bmp);
picture = tempFile;
setBMPAsDesktop(picture);
File.Delete(tempFile);
}
else
{
setBMPAsDesktop(picture);
}
}
}
/// <summary>
/// 设置BMP格式的背景图片
/// </summary>
/// <param name="bmp">图片路径</param>
private void setBMPAsDesktop(string bmp)
{
SystemParametersInfo(20, 0, bmp, 0x2);
}
}
}
附件是编译好的exe文件和源码。
分享到:
相关推荐
python2爬取bing主页并作为桌面背景,压缩包中包含py2.7的win32gui,供未安装者使用,且代码第一行注释即其下载地址,若为py3.x可从中下载,py3需要改为urllib.request 使用。
微软的新搜索引擎Bing每天都会更新一副精美的背景图片,如果能将每幅图片统统收集起来,简直就是一巨大的图片资料库,往后必有用到之处。但是Bing只能显示当天的背景图片,以往用过的图片就无法找到,而且背景图片...
Bing壁纸获取并设置桌面背景,实现壁纸保存,定时切换桌面背景
微软的新搜索引擎Bing每天都会更新一副精美的背景图片,如果能将每幅图片统统收集起来,简直就是一巨大的图片资料库,往后必有用到之处。但是Bing只能显示当天的背景图片,以往用过的图片就无法找到,而且背景图片...
每日自动更换必应bing桌面壁纸_pybingwp_BingWallpaper 必应每日高清壁纸,自动更换必应桌面壁纸,每天自动下载必应Bing精美图片并更换桌面壁纸小工具,可以设置所在国、后台驻留与定时自动更换。
网上找到的在python3下都不能用,于是动手修改了一下,测试可用。 Python 版本:3.5.1 系统:win10 x64
windows下,双击即可设置bing每日图片为桌面壁纸,可以将快捷方式放入开机启动文件夹中实现开机切换壁纸。
必应每日高清壁纸,自动更换必应桌面壁纸,每天自动下载必应Bing精美图片并更换桌面壁纸小工具,可以设置所在国、后台驻留与定时自动更换。
易语言获取每日bing背景例子,给大家看看代码
利用WebRequest和WebResponse及文件操作方法下载bing的背景图片。
原创,
bing每日12点自动更换壁纸!可选择每个国家.
安装 Bing Desktop (必应桌面) 之后,它会每天自动将 Bing 的每日壁纸设置成你的桌面,一般都是 1920×1200 分辨率的高清壁纸,基本能满足市面上所有流行的显示器尺寸了。此外,它还提供了便捷的搜索框,你可以通过 ...
有需要学习的,可以要求源码哦!就是可以下载Bing每日的桌面。
Win8系统下自动开启Bing的解决方法.docx
必应词典桌面版是由微软亚洲研究院研发的新一代在线词典软件。结合了互联网在线词典及桌面词典的优势,依托必应搜索引擎技术,及时发现并收录网络新兴词汇,让您的词典永不过时。基于微软强大的技术实力和创新能力,...
通过正则表达式 自动下载保存bing.com背景图片到一个文件夹内
bing桌面图片,bing桌面图片,bing桌面图片
BingToday一直用的Bing Desktop最近发现有问题了,为了自动扒桌面图片,造了个简单的轮子。功能:1、每日自动将桌面背景更换为Bing.com的背景图。2、自动跟随电脑启动。Just run and go.