轻松创建壁纸!






3.50/5 (6投票s)
处理不同的图片(格式/大小),并将它们很好地安装在您的桌面 - 壁纸 - 上

引言
这是我的第二篇文章。 我试图在这篇文章中帮助一些程序员处理图形概念,所以我以壁纸桌面作为练习。
背景
我想,这篇文章中唯一困难的事情是 API 的使用! 因此,我添加了一份文档,解释如何使用此 API(API 解释文件)。
Using the Code
由于我们的项目范围比较广(当然,比之前的项目),我将重点放在处理图片和创建壁纸的类上。
所以让我们开始吧
- 首先要做的是初始化用作“主”壁纸的图片,另一方面,控制此过程的属性是
MyFileBimap
。Friend Property MyFileBimap() As String Get Return MyFileNameEx End Get Set(ByVal value As String) If value Is Nothing OrElse value = "" Then MyFileBimapEx = Nothing MyFileNameEx = "" Else MyFileBimapEx = New Bitmap(value) MyFileNameEx = value End If End Set End Property
- 现在,我假设我的图片真的很“小”,因为我的屏幕分辨率非常大。 所以我有一个想法,在这个图片中包含一个背景。 好吧,我必须注意到,如果图片不存在,我的项目将直接创建一个黑色背景。 这就是将初始化背景图片的属性。
Friend Property MyBgPic() As Bitmap Get Return MyBgPicEx End Get Set(ByVal value As Bitmap) If value Is Nothing Then MyBgPicEx = Nothing Else If value.Width > MyScreenWidth _ OrElse value.Height > MyScreenHeight Then 'Set the size of the back-ground pic 'as the size of screen resolution MyBgPicEx = New Bitmap(value, MyScreenWidth, MyScreenHeight) Else MyBgPicEx = value End If End If End Set End Property
此属性将创建与屏幕分辨率匹配的背景位图!
您可能想知道我为什么添加这张背景图片。 老实说,你们喜欢这种壁纸吗?

还是你更喜欢这个

我的意思是,当系统尝试将图片调整到桌面(取决于您的屏幕分辨率)时,您的图片看起来会真的很模糊!
所以,这个想法是保持原始图片的大小,并将这个图片添加到更大的背景中。
现在,我们可以调用创建最终图片的子程序 WallpaperChanger
Friend Sub WallpaperChanger()
Dim MyXLoc As Int32
Dim MyYLoc As Int32
'
Dim MyGraphic As Graphics
'
If MyBgPicEx Is Nothing Then
MyBgPicEx = New Bitmap(MyScreenWidth, MyScreenHeight)
MyGraphic = Graphics.FromImage(MyBgPicEx)
'Fill the empty area !...in my example i fill it with Black brush
MyGraphic.FillRectangle(Brushes.Black, 0, 0, MyScreenWidth, MyScreenHeight)
Else
'Resize the background picture...
MyBgPicEx = New Bitmap(MyBgPicEx, MyScreenWidth, MyScreenHeight)
MyGraphic = Graphics.FromImage(MyBgPicEx) 'Create a graphic from MyBgPicEx,
'in which will be added our MyFileBitmapEx
End If
如您所见,如果未输入背景图片,程序将创建一个空的黑色区域,其高度/宽度等于屏幕分辨率,另一方面,当 (MyBgPicEx
<> Nothing) 时,将发生调整大小操作。
'Verify the width & height of the picture
If MyFileBimapEx.Width > MyScreenWidth Then
MyXLoc = 0
MyFileBimapEx = New Bitmap_
(MyFileBimapEx, MyScreenWidth, MyFileBimapEx.Height)
Else
MyXLoc = (MyScreenWidth - MyFileBimapEx.Width) / 2
End If
If MyFileBimapEx.Height > MyScreenHeight Then
MyYLoc = 0
MyFileBimapEx = New Bitmap_
(MyFileBimapEx, MyFileBimapEx.Height, MyScreenHeight)
Else
MyYLoc = (MyScreenHeight - MyFileBimapEx.Height) / 2
End If
那么现在 MyXLoc
& MyYLoc
怎么样!

这两个变量仅在 (MyFileBitmapEx
) 小于屏幕分辨率时存在,并且都给出了 MyFileBitmapEx
在 MyBgPicEx.
中的位置 (x,y)。
'Draw the FileMyBitmapEx into the MyBgPicEx, in the given location (MyXLoc, MyYLoc) !
MyGraphic.DrawImage(MyFileBimapEx, _
MyXLoc, MyYLoc, MyFileBimapEx.Width, MyFileBimapEx.Height)
- 想象一下,有人想在壁纸上添加一些文本,... 由于
MyGraphic
包含 (MyBgPicEx
+MyFileBitmapEx
),我们可以添加一些文本If MyTextEx.Length > 0 Then MyGraphic.DrawString(MyTextEx, MyFontStyleEx, _ New SolidBrush(MyFontColorEx), MyTextLocEx.X, MyTextLocEx.Y) End If
- 由于所有效果都添加到
MyGraphic
,我们现在能够生成我们的位图,并且我必须注意到,当您调用SavePicture
时它将被保存到磁盘,但在那之前,让我们检查WallpaperChanger
子程序的最后一部分。'Create a new pathfile Dim MyStr As String MyStr = System.IO.Path.GetFileNameWithoutExtension(MyFileNameEx) MyNewPath = My.Computer.FileSystem.CombinePath(TempDirEx, MyStr & ".bmp") 'Verif that file doesn't exist If IO.File.Exists(MyNewPath) Then Dim DialogResult As MsgBoxResult = _ MsgBox("Do you want to replace the existing file", _ MsgBoxStyle.Exclamation + MsgBoxStyle.YesNo, "Notification") If DialogResult = MsgBoxResult.No Then MyStr = InputBox("filename", "Enter a valid filename !", _ "", MyScreenWidth / 2, MyScreenHeight / 2) MyNewPath = My.Computer.FileSystem.CombinePath_ (TempDirEx, MyStr & ".bmp") End If End If 'Return the path, so it can be loaded again in preview picture box MyFileNameEx = MyNewPath
如您所见,最后一部分只生成结果图片的名称,该图片将最终使用
SavedPicture
保存到磁盘Friend Sub SavePicture() 'Save bitmap to disk... MyBgPicEx.Save(MyFileNameEx, Drawing.Imaging.ImageFormat.Bmp) End Sub
- 这个项目中**有趣**的部分是如何设置壁纸,我通过调用一个声明为的 API 函数来完成
Const SPI_SETDESKWALLPAPER = 20 Const SPIF_UPDATEINIFILE = &H1 Const SPIF_SENDWININICHANGE = &H2 Private Declare Auto Function SystemParametersInfo Lib _ "user32" (ByVal uAction As Int32, ByVal uParam As Int32, _ ByVal lpvParam As String, ByVal fuWinIni As Int32) As Integer
并从
SetWallpaper
中调用Friend Sub SetWallpaper(ByVal MyBitmap As String) SystemParametersInfo(SPI_SETDESKWALLPAPER, _ 0, MyBitmap, SPIF_UPDATEINIFILE Or SPIF_SENDWININICHANGE) End Sub
-
是的,正如您所猜测的那样:
MyBitmap
是从之前的转换中产生的图片。
这个项目中的其他内容
嗯,我需要很多时间来解释其他内容,例如
- 在给定的时间后更改桌面壁纸,当然我使用了一个计时器
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles Timer1.Tick If MyCount > Me.ListBox1.Items.Count - 1 Then MyCount = 0 Else MyWallpaper.SetWallpaper(ListBox1.Items.Item(MyCount)) MyCount += 1 End If End Sub
- 选择在图片上放置文本的位置
Private Sub PictureBox3_MouseClick(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) _ Handles PictureBox3.MouseClick Select Case e.Button Case Windows.Forms.MouseButtons.Left 'Store the current position of text MyWallpaper.MyTextLoc = New Point(e.X * Me.ScreenRsX / 232, _ e.Y * Me.ScreenRsY / 201) TextBox2.Text = Format((e.X * Me.ScreenRsX / 232), "0") TextBox3.Text = Format((e.Y * Me.ScreenRsY / 201), "0") Case Windows.Forms.MouseButtons.Right 'Show the resulted wallpaper in fullscreen mode ! Dim MyResizer As New Bitmap(Me.PictureBox3.Image) FullScreen.PictureBox1.Image = Me.PictureBox3.Image FullScreen.Height = MyResizer.Height FullScreen.Width = MyResizer.Width ' FullScreen.Show() MyResizer = Nothing End Select End Sub
好吧,为了清楚起见,当单击右键时,结果壁纸将以全屏模式显示。
另一件事,我添加了一个反转颜色子程序。 有人可能想在图片上添加白色文本,并且由于文本框也有一个白色背景... 此子程序将反转颜色,以便用户能够看到他写的内容!
Private Function RevertColor(ByVal MyColor As Color) As Color Dim MyAlpha As Int32 Dim MyRGBCvrted As Int32 MyAlpha = MyColor.ToArgb And &HFF000000 'Store alpha... MyRGBCvrted = MyColor.ToArgb And &HFFFFFF _ 'Now eliminate Alpha...and keep only RBG colors MyRGBCvrted = &HFFFFFF - MyRGBCvrted 'Revert color MyRGBCvrted += MyAlpha 'Restore Alpha Return Color.FromArgb(MyRGBCvrted) End Function
关注点
我尽最大努力使这个项目清晰,我希望您确实找到了一些有用的东西,这些东西将对您的项目有所帮助。 对于我来说,这是我使用 .NET 提供的 GDI+ 功能的另一次很好的入门!
历史
- 版本 (1.0.0) 创建于:(26).03.2008 作者:GENCOX {Mozads}