csharpプログラミング

csharpの勉強用に,北海道放送専門天気図のデータをダウンロードして,そのデータをPCの壁紙として貼り付けるプログラムを作ってみた.

// weather.cs

using System;
using System.Threading;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Net;
using System.Text;
using System.Runtime.InteropServices;

public class Weather
{
	[DllImport("user32.dll")]
	public static extern int SystemParametersInfo (
		int uAction,
		int uParam,
		[MarshalAs(UnmanagedType.LPStr)] string lpvParam,
		int fuWinIni);

	static void Main()
	{
		string imageURL = "http://www.hbc.co.jp/tecweather/ASAS.jpg";
		string toFile   = "asas.bmp";

		WebRequest req = WebRequest.Create(imageURL);
		WebResponse res = req.GetResponse();
		Stream rec = res.GetResponseStream();
		Bitmap bitmap = new Bitmap(rec);
		bitmap.Save(toFile, ImageFormat.Bmp);
		bitmap.Dispose();
		SystemParametersInfo(20, 0, Path.GetFullPath(toFile), 0x1|0x2);
	}
}

jpegファイルをダウンロードした後,bitmapに変換してファイルに保存.その後で,SystemParametersInfo というwin32APIを使って画面を設定する.