WinForm开机启动、禁用开启任务管理器图片压缩常用函数
WinForm开机启动、图片压缩等常用函数--------------------------------------痴心长剑.W技术文档系列
using System;
using System.Collections.Generic; using System.Text;
using Microsoft.Win32;
using System.Diagnostics;
using System.Runtime.InteropServices; using System.Windows.Forms;
using System.Drawing;
namespace Ecan
{
public class EcanSystem
{
///
/// 设置程序开机运行
///
///
是否开机运行
///
要运行的EXE程序名称(不要拓展名) ///
要运行的EXE程序路径
///
成功返回真,否则返回假
public bool runWhenStart(bool started, string exeName, string path)
{
Copyright ? 痴心长剑.W 2012.05.09
WinForm开机启动、图片压缩等常用函数--------------------------------------痴心长剑.W技术文档系列
RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//打
开注册
子项
if (key == null)//如果该项不存在的话,则创建该子项
{
key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
}
if (started == true)
{
try
{
key.SetValue(exeName, path);//设置为开机启动
key.Close();
}
catch
{
return false;
}
}
else
{
try
{
Copyright ? 痴心长剑.W 2012.05.09
WinForm开机启动、图片压缩等常用函数--------------------------------------痴心长剑.W技术文档系列
key.DeleteValue(exeName);//取消开机启动
key.Close();
}
catch
{
return false;
}
}
return true;
}
///
/// 解禁任务管理器
///
///
成功返回真,否则返回假
public bool enableTaskmgr() {
RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system",
true);//打开注册表子项
if (key == null)//如果该项不存在的话,则创建该子项
{
key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system");
Copyright ? 痴心长剑.W 2012.05.09
WinForm开机启动、图片压缩等常用函数--------------------------------------痴心长剑.W技术文档系列
}
try
{
key.SetValue("disabletaskmgr", 0, RegistryValueKind.DWord);
key.Close();
return true;
}
catch
{
return false;
}
}
///
/// 禁用任务管理器
///
///
成功返回真,否则返回假
public bool notEnableTaskmgr() {
RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system",
true);//打开注册表子项
if (key == null)//如果该项不存在的话,则创建该子项
{
Copyright ? 痴心长剑.W 2012.05.09
WinForm开机启动、图片压缩等常用函数--------------------------------------痴心长剑.W技术文档系列
key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\syste
m");
}
try
{
key.SetValue("disabletaskmgr", 1, RegistryValueKind.DWord); key.Close();
return true;
}
catch
{
return false;
}
Copyright ? 痴心长剑.W 2012.05.09
WinForm开机启动、图片压缩等常用函数--------------------------------------痴心长剑.W技术文档系列
}
///
/// 解禁注册表
///
///
成功返回真,否则返回假
public bool enableRegedit()
{
RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\polici
es\\system", true);//打开注册表子项
if (key == null)//如果该项不存在的话,则创建该子项
{
key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\syste
m");
Copyright ? 痴心长剑.W 2012.05.09
WinForm开机启动、图片压缩等常用函数--------------------------------------痴心长剑.W技术文档系列
}
try
{
key.SetValue("disableregistrytools", 0, RegistryValueKind.DWord);
key.Close();
return true;
}
catch
{
return false;
}
}
///
Copyright ? 痴心长剑.W 2012.05.09
WinForm开机启动、图片压缩等常用函数--------------------------------------痴心长剑.W技术文档系列
/// 禁用注册表
///
///
成功返回真,否则返回假
public bool notEnableRegedit()
{
RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\polici
es\\system", true);//打开注册表子项
if (key == null)//如果该项不存在的话,则创建该子项
{
key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\syste
m");
}
try
Copyright ? 痴心长剑.W 2012.05.09
WinForm开机启动、图片压缩等常用函数--------------------------------------痴心长剑.W技术文档系列
{
key.SetValue("disableregistrytools", 1, RegistryValueKind.DWord);
key.Close();
return true;
}
catch
{
return false;
}
}
///
/// 结束进程
///
Copyright ? 痴心长剑.W 2012.05.09
WinForm开机启动、图片压缩等常用函数--------------------------------------痴心长剑.W技术文档系列
///
进程名称
///
成功返回真,否则返回假
public bool killProcess(string processName) {
try
{
foreach (Process p in Process.GetProcesses()) {
if (p.ProcessName == processName)
{
p.Kill();
}
}
Copyright ? 痴心长剑.W 2012.05.09
WinForm开机启动、图片压缩等常用函数--------------------------------------痴心长剑.W技术文档系列
return true;
}
catch { return false; }
}
///
/// 注册控件
///
///
控件
注册后对应的键值
///
成功返回真,否则返回假
public bool regDll(string dllIdValue) {
try
Copyright ? 痴心长剑.W 2012.05.09
WinForm开机启动、图片压缩等常用函数--------------------------------------痴心长剑.W技术文档系列
{
RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"CLSTD\" + dllIdValue, true);//打开注册表子项 if (key == null)//如果该项不存在的话,则创建该子项
{
key = Registry.ClassesRoot.CreateSubKey(@"CLSTD\" + dllIdValue);
}
return true;
}
catch { return false; }
}
///
/// 压缩图片(指定压缩比例值)
///
Copyright ? 痴心长剑.W 2012.05.09
WinForm开机启动、图片压缩等常用函数--------------------------------------痴心长剑.W技术文档系列
///
源文件
///
保存文件
///
比例值(例如0.5)
///
成功返回真,否则返回假
public bool pressImage(string fromFile, string saveFile,double bili)
{
Image img;
Bitmap bmp;
Graphics grap;
int width, height;
try
{
img = Image.FromFile(fromFile);
Copyright ? 痴心长剑.W 2012.05.09
WinForm开机启动、图片压缩等常用函数--------------------------------------痴心长剑.W技术文档系列
width = Convert.ToInt32(img.Width * bili);
height = Convert.ToInt32(img.Height * bili);
bmp = new Bitmap(width, height);
grap = Graphics.FromImage(bmp);
grap.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; grap.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
grap.DrawImage(img, new Rectangle(0, 0, width, height)); bmp.Save(saveFile, System.Drawing.Imaging.ImageFormat.Jpeg); grap.Dispose();
bmp.Dispose();
img.Dispose();
return true;
}
Copyright ? 痴心长剑.W 2012.05.09
WinForm开机启动、图片压缩等常用函数--------------------------------------痴心长剑.W技术文档系列
catch { return false; }
}
///
/// 压缩图片(指定高度和宽度)
///
///
源文件
///
保存文件
///
宽度值
///
高度值
///
成功返回真,否则返回假
public bool pressImage(string fromFile, string saveFile, int width,int height)
{
Image img;
Copyright ? 痴心长剑.W 2012.05.09
WinForm开机启动、图片压缩等常用函数--------------------------------------痴心长剑.W技术文档系列
Bitmap bmp;
Graphics grap;
try
{
img = Image.FromFile(fromFile);
bmp = new Bitmap(width, height);
grap = Graphics.FromImage(bmp);
grap.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; grap.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
grap.DrawImage(img, new Rectangle(0, 0, width
, height));
bmp.Save(saveFile, System.Drawing.Imaging.ImageFormat.Jpeg);
Copyright ? 痴心长剑.W 2012.05.09
WinForm开机启动、图片压缩等常用函数--------------------------------------痴心长剑.W技术文档系列
grap.Dispose();
bmp.Dispose();
img.Dispose();
return true;
}
catch { return false; }
}
}
}
Copyright ? 痴心长剑.W 2012.05.09