VB.NET图像处理入门
作者:admin 日期:2009-01-10
基础绘图
拖个按钮到Form1上.
双击按钮, 在里面输入
Dim gg As Graphics
gg = Graphics.FromHwnd(Me.Handle)
gg.DrawRectangle(Pens.AliceBlue, 0, 0, 100, 100)
结束后Debug, 按下按钮就会画一个方形
Dim gg as Graphics 是在定义一个图像处理器,名叫gg
gg = Graphics.FromHwnd(Me.Handle) 是在指定画在什么地方
这前两句是初始化用
常用的有:
用FromHwnd来画在组件上...Me.Handle代表是直接画在Form1上, 也就是本Class
用FromImage来画在组件的图片属性上, 如FromImage(Me.Backgroundimage)
注意如果这样做, 本来的Backgroundimage里必须有东西,不能是null..否则会有错.
gg.drawRectangle(画笔颜色, x, y, 宽,高)
另gg.下还会出现很多别的东西,如DrawElipse来画椭圆, 用DrawImage来画程序外的图片..
DrawImageUnscaled意味着会以原始大小来画图, 速度更快..
要画程序外的图片可以直接画的时候加载:
gg.drawimage(Image.FromFile("路径"), 画图位置)
如: gg.drawimage(image.fromfile("c:\aaa.bmp"), 0,0)
在gg所定义上的组件的0,0的位置画上aaa.bmp
也可以先加载:
dim bp as bitmap
bp = image.fromfile("c:\aaa.bmp")
gg.drawimage(bp, 0,0)
gg.drawimage(图片, x,y, 宽,高) 可以控制图片的大小,如不填宽高则按照原来尺寸画出
gg.drawimageunscaled则不能控制大小
另有FillRectangle和一些其他Fill开头的可以画实心的图形.
可以把一个bitmap弄得透明:
dim bp as bitmap
bp.MakeTransparent()
如果不在MakeTransparent中放任何值, 系统将会选择图像左上角的像素颜色作为透明色,否则将会使用我们在MakeTransparent里送入的颜色值。
动画概念
在VB.NET中让一个画面成为动态, 无非包括这样一个过程: 绘制 - 擦除- 绘制
所谓擦除有两个方法, 一是用别的图覆盖, 二是在intptr画的图(以gg.fromhwnd(****)初始化的绘制工具) 只要用refresh()的命令就能清除.
为了速度与流畅, 最好用覆盖的方法
如果只是为了程序简单,则用refresh()
Double Buffer 与 BitBlt
这两个不需要用到,当时我在网上学的很模糊,也可能有错.根据模糊的记忆..
BitBlt是在屏幕上看不见的地方画上一些常用图, 然后在需要用这些图的时候直接从屏幕的一个地方抄到另一个地方,这样比直接绘制的速度要快很多.
Double Buffer是把即将显示的帧画好, 然后显示的时候开始画后面的帧...
帧:Frame, 动画的一页
在VB.NET中, 似乎BitBlt已经是自动化的了, 而一个Form上通常会有Double Buffer [True/False]的属性供选择...我试了下, 看不出效果...
像素处理
像素的处理主要有三个部分:得到特定点的像素信息,分析像素信息, 写入特定点的像素信息
首先把一个图像用Bitmap加载到内存。比如: image.fromfile("c:\aaa.bmp"), 或者已经在程序里的图像, 如me.BackgroundImage, 这里我们用前者作为例子。
Dim pic As Bitmap
pic = image.fromfile("c:\aaa.bmp")
然后我们得到pic的一个像素的RGB。 因为RGB是长整数, 所以我们首先建立一个long, 然后得到pic在坐标为(x,y)的点, 然后把这个点上的RGB存到这个long里面去。
Dim clr As Long
clr = pic.getPixel(x,y).ToArgb
现在我们需要分析这个RGB, 把它转换成R, G, 和B, 算法如下:
Dim r As integer
Dim g As integer
Dim b As integer
b = 255 + clr \ 256 \ 256
g = 255 + (clr \ 256) Mod 256
r = 255 + clr Mod 256
如果要设定一个像素的到这个颜色信息, 我们可以这样做:
pic.SetPixel(x, y, System.Drawing.Color.FromArgb(r, g, b))
System.Drawing.Color.FromArgb(r,g,b)会给我们r,g,b所代表的颜色。
颜色对比:
看颜色是否有差别, 或者差别有多大, 在处理图像中可以用来识别图像中突出的颜色或指定的颜色。
我在制作一个软件的过程中弄了三种颜色对比方法, 我们从最初始和幼稚的看起, 一直到最后非常完善的。
这里, 我们先定义几个整数值:
r1, g1, b1, r2, g2, b2, t
t是对比允许差别度, 其他的是两个颜色的r, g, b
(1)Absolute RGB
我们把r1-r2 看其绝对值是否小于 t, g1-g2, b1-b2...只要有一个不是小于t, 我们说这两个像素是不同的.
if math.abs(r1-r2)>t or math.abs(g1-g2)>t or math.abs(b1-b2)>t then
return false
end if
return true
(2)Average RGB
这里我们把所有的混起来加减了取平均, 其实取不取平均都随便, 反正是把RGB非常没有道理地加起来吧.
if math.abs(r1+g1+b1-r2-g2-b2)/3 > t then
return false
end if
return true
(3)3D Path
这里我们把r, g, b当作一个三维的空间, 看两个颜色间的距离。这是到现在为止最准确的方法, 虽然有可能可以直接对比开始得到的long, 但是那还需要试验。这个办法是绝对准确的:
dim distance as single
distance = Math.Sqrt(Math.Pow(r1-r2, 2) + Math.Pow(g1-g2, 2) + Math.Pow(b1-b2, 2))
if distance > t then
return false
end if
return true
图片保存
Dim b As Bitmap = 想要保存的图片。
b.Save("c:\picture.bmp", System.Drawing.Imaging.ImageFormat.Jpeg)
左边的是文件名, 可以带文件路径,右边的是保存格式, 这里选择为保存成jpeg
以上就是最基础的VB.NET图像处理方法了。
c#上传文件并限制上传文件类型
作者:admin 日期:2009-01-07
下面的示例演示如何创建 FileUpload 控件,该控件将文件保存到代码中指定的路径。该示例只允许上载扩展名为 .doc 或 .xls 的文件。调用 Path.GetExtension 方法来返回要上载的文件的扩展名。如果文件扩展名为 .doc 或 .xls,则调用 SaveAs 方法将文件保存到服务器上的指定路径。
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void UploadBtn_Click(object sender, EventArgs e)
{
// Specify the path on the server to
// save the uploaded file to.
string savePath = @"c:\temp\uploads";
// Before attempting to save the file, verify
// that the FileUpload control contains a file.
if (FileUpload1.HasFile)
{
// Get the name of the file to upload.
string fileName = Server.HtmlEncode(FileUpload1.FileName);
// Get the extension of the uploaded file.
string extension = System.IO.Path.GetExtension(fileName);
// Allow only files with .doc or .xls extensions
// to be uploaded.
if ((extension == ".doc") | (extension == ".xls"))
{
// Append the name of the file to upload to the path.
savePath += fileName;
// Call the SaveAs method to save the
// uploaded file to the specified path.
// This example does not perform all
// the necessary error checking.
// If a file with the same name
// already exists in the specified path,
// the uploaded file overwrites it.
FileUpload1.SaveAs(savePath);
// Notify the user that their file was successfully uploaded.
UploadStatusLabel.Text = "Your file was uploaded successfully.";
}
else
{
// Notify the user why their file was not uploaded.
UploadStatusLabel.Text = "Your file was not uploaded because " +
"it does not have a .doc or .xls extension.";
}
}
}
</script>
<html >
<head runat="server">
<title>FileUpload Class Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h4>Select a file to upload:</h4>
<asp:FileUpload id="FileUpload1"
runat="server">
</asp:FileUpload>
<br/><br/>
<asp:Button id="UploadBtn"
Text="Upload file"
OnClick="UploadBtn_Click"
runat="server">
</asp:Button>
<hr />
<asp:Label id="UploadStatusLabel"
runat="server">
</asp:Label>
</div>
</form>
</body>
</html>
c#上传文件并限制上传文件大小
作者:admin 日期:2009-01-07
面的示例演示如何创建 FileUpload 控件,该控件将文件保存到代码中指定的路径。该控件将上载文件的大小限制为 5 MB。使用 PostedFile 属性来访问基础 ContentLength 属性并返回文件的大小。如果要上载的文件的大小小于 2 MB,则调用 SaveAs 方法将文件保存到服务器上的指定路径。除了检查应用程序代码中的最大文件大小设置之外,您还可以将 httpRuntime 元素的 maxRequestLength 属性设置为应用程序配置文件中所允许的最大大小。
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void UploadButton_Click(object sender, EventArgs e)
{
// Specify the path on the server to
// save the uploaded file to.
string savePath = @"c:\temp\uploads\";
// Before attempting to save the file, verify
// that the FileUpload control contains a file.
if (FileUpload1.HasFile)
{
// Get the size in bytes of the file to upload.
int fileSize = FileUpload1.PostedFile.ContentLength;
// Allow only files less than 2,100,000 bytes (approximately 2 MB) to be uploaded.
if (fileSize < 2100000)
{
// Append the name of the uploaded file to the path.
savePath += Server.HtmlEncode(FileUpload1.FileName);
// Call the SaveAs method to save the
// uploaded file to the specified path.
// This example does not perform all
// the necessary error checking.
// If a file with the same name
// already exists in the specified path,
// the uploaded file overwrites it.
FileUpload1.SaveAs(savePath);
// Notify the user that the file was uploaded successfully.
UploadStatusLabel.Text = "Your file was uploaded successfully.";
}
else
{
// Notify the user why their file was not uploaded.
UploadStatusLabel.Text = "Your file was not uploaded because " +
"it exceeds the 2 MB size limit.";
}
}
else
{
// Notify the user that a file was not uploaded.
UploadStatusLabel.Text = "You did not specify a file to upload.";
}
}
</script>
<html >
<head runat="server">
<title>FileUpload Class Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h4>Select a file to upload:</h4>
<asp:FileUpload id="FileUpload1"
runat="server">
</asp:FileUpload>
<br/><br/>
<asp:Button id="UploadButton"
Text="Upload file"
OnClick="UploadButton_Click"
runat="server">
</asp:Button>
<hr />
<asp:Label id="UploadStatusLabel"
runat="server">
</asp:Label>
</div>
</form>
</body>
</html>
c#上传文件并保存到指定目录
作者:admin 日期:2009-01-07
下面的示例演示如何创建 FileUpload 控件,该控件将文件保存到文件系统中针对应用程序的指定目录。使用 HttpRequest.PhysicalApplicationPath 属性来获取当前正在执行的服务器应用程序的根目录的物理文件系统路径。调用 SaveAs 方法将文件保存到服务器上的指定路径。
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void UploadButton_Click(object sender, EventArgs e)
{
// Save the uploaded file to an "Uploads" directory
// that already exists in the file system of the
// currently executing ASP.NET application.
// Creating an "Uploads" directory isolates uploaded
// files in a separate directory. This helps prevent
// users from overwriting existing application files by
// uploading files with names like "Web.config".
string saveDir = @"\Uploads\";
// Get the physical file system path for the currently
// executing application.
string appPath = Request.PhysicalApplicationPath;
// Before attempting to save the file, verify
// that the FileUpload control contains a file.
if (FileUpload1.HasFile)
{
string savePath = appPath + saveDir +
Server.HtmlEncode(FileUpload1.FileName);
// Call the SaveAs method to save the
// uploaded file to the specified path.
// This example does not perform all
// the necessary error checking.
// If a file with the same name
// already exists in the specified path,
// the uploaded file overwrites it.
FileUpload1.SaveAs(savePath);
// Notify the user that the file was uploaded successfully.
UploadStatusLabel.Text = "Your file was uploaded successfully.";
}
else
{
// Notify the user that a file was not uploaded.
UploadStatusLabel.Text = "You did not specify a file to upload.";
}
}
</script>
<html >
<head runat="server">
<title>FileUpload Class Example</title>
</head>
<body>
<h3>FileUpload Class Example: Save To Application Directory</h3>
<form id="form1" runat="server">
<div>
<h4>Select a file to upload:</h4>
<asp:FileUpload id="FileUpload1"
runat="server">
</asp:FileUpload>
<br/><br/>
<asp:Button id="UploadButton"
Text="Upload file"
OnClick="UploadButton_Click"
runat="server">
</asp:Button>
<hr />
<asp:Label id="UploadStatusLabel"
runat="server">
</asp:Label>
</div>
</form>
</body>
</html>
c#上传文件
作者:admin 日期:2009-01-07
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void UploadButton_Click(object sender, EventArgs e)
{
// Specify the path on the server to
// save the uploaded file to.
String savePath = @"c:\temp\uploads\";
// Before attempting to perform operations
// on the file, verify that the FileUpload
// control contains a file.
if (FileUpload1.HasFile)
{
// Get the name of the file to upload.
String fileName = FileUpload1.FileName;
// Append the name of the file to upload to the path.
savePath += fileName;
// Call the SaveAs method to save the
// uploaded file to the specified path.
// This example does not perform all
// the necessary error checking.
// If a file with the same name
// already exists in the specified path,
// the uploaded file overwrites it.
FileUpload1.SaveAs(savePath);
// Notify the user of the name of the file
// was saved under.
UploadStatusLabel.Text = "Your file was saved as " + fileName;
}
else
{
// Notify the user that a file was not uploaded.
UploadStatusLabel.Text = "You did not specify a file to upload.";
}
}
</script>
<html >
<head runat="server">
<title>FileUpload Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h4>Select a file to upload:</h4>
<asp:FileUpload id="FileUpload1"
runat="server">
</asp:FileUpload>
<br /><br />
<asp:Button id="UploadButton"
Text="Upload file"
OnClick="UploadButton_Click"
runat="server">
</asp:Button>
<hr />
<asp:Label id="UploadStatusLabel"
runat="server">
</asp:Label>
</div>
</form>
</body>
</html>
C#中的cookie编程
作者:admin 日期:2009-01-06
Cookie就是所谓的" 小甜饼" ,他最早出现是在Netscape Navigator 2.0中。Cookie其实就是由Web服务器创建的、将信息存储在计算机上的文件。那么为什么Web服务器要在客户机上面创建如此文件?这是因为当客户机发送一个请求到WEB服务器时(譬如准备浏览页面时),无论此客户机是否是第一次来访,服务器都会把它当作第一次来对待,WEB服务器所做的工作只是简单的进行响应,然后就关闭与该用户的连接。这样处理过程所带来的缺点时显而易见的。自从网景公司开发出Cookie以后,就可以利用Cookie来保存用户的识别信息。Cookie的作用可以记录了您在该站点上曾经访问过的页面,由此帮助您下次访问该站点时自定义查看。Cookies 也可以存储个人可识别信息。个人可识别信息是可以用来识别或联系您的信息,例如姓名、电子邮件地址、家庭或工作地址,或者电话号码。然而,网站只能访问您提供的个人可识别信息。例如,除非您提供电子邮件名称,否则网站将不能确定您的电子邮件名称。另外,网站不能通过Cookie来访问计算机上的其他信息。当然除非你提供。那么Cookie到底存放在什么地方?如果机器的系统是视窗98且安装在" C" 盘中,那么Cookie存放在" C:\Windows\Cookies" 目录中;如果机器系统是视窗2000且安装在" C" 盘中,那么Cookie存放在" C:\Documents and Settings\Administrator\Cookies" 目录中。了解了Cookie这么多知识,我们还是来了解一下本文的重点-- C#是如何进行Cookie方面编程的。主要内容有二点:其一是 C#是如何写入Cookie;其二是 C#是如何访问自己写入的Cookie。
一、本文介绍的程序设计和运行的软件环境:
微软公司视窗2000服务器版
.Net FrameWork SDK Beta 2
C#进行Cookie方面编程是通过ASP.NET页面来实现的。
C# 如何将string 转换为 Color 类型
作者:admin 日期:2009-01-06
c#正则表达式验证字符串
作者:admin 日期:2009-01-06
下面的代码示例演示如何使用正则表达式检查字符串是否具有表示货币值的正确格式。注意,如果使用 ^ 和 $ 封闭标记,则指示整个字符串(而不只是子字符串)都必须匹配正则表达式。
using System;
using System.Text.RegularExpressions;
public class Test
{
public static void Main ()
{
// Define a regular expression for currency values.
Regex rx = new Regex(@"^-?\d+(\.\d{2})?$");
// Define some test strings.
string[] tests = {"-42", "19.99", "0.001", "100 USD"};
// Check each test string against the regular expression.
foreach (string test in tests)
{
if (rx.IsMatch(test))
{
Console.WriteLine("{0} is a currency value.", test);
}
else
{
Console.WriteLine("{0} is not a currency value.", test);
}
}
}
}
常用正则表达式
作者:admin 日期:2009-01-05
下面是我我以前找的常用正则表达式。根据自己需要进行组合。
"^\d+$" //非负整数(正整数 + 0)
"^[0-9]*[1-9][0-9]*$" //正整数
"^((-\d+)|(0+))$" //非正整数(负整数 + 0)
"^-[0-9]*[1-9][0-9]*$" //负整数
"^-?\d+$" //整数
"^\d+(\.\d+)?$" //非负浮点数(正浮点数 + 0)
"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$" //正浮点数
"^((-\d+(\.\d+)?)|(0+(\.0+)?))$" //非正浮点数(负浮点数 + 0)
"^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$" //负浮点数
"^(-?\d+)(\.\d+)?$" //浮点数
"^[A-Za-z]+$" //由26个英文字母组成的字符串
"^[A-Z]+$" //由26个英文字母的大写组成的字符串
"^[a-z]+$" //由26个英文字母的小写组成的字符串
"^[A-Za-z0-9]+$" //由数字和26个英文字母组成的字符串
"^\w+$" //由数字、26个英文字母或者下划线组成的字符串
"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$" //email地址
"^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$" //url
c#连接Mysql数据库 Connect to mySQL in C#
作者:admin 日期:2009-01-05
Instructions:
1) Download and install the connector: url=http://dev.mysql.com/downloads/connector/net/1.0.html
2) Create a test project
3)Next add reference to: MySql.Data
4) Next add "using MySql.Data.MySqlClient;"
/// <summary>
/// Method for connection to a mySQL database. You
/// need to download the mySQL Connector located at
/// http://dev.mysql.com/downloads/connector/net/1.0.html]
///
/// NOTE: I am a big supporter of having the connection
/// stored in the web.config, not inline like this
/// </summary>
/// <param name="server"></param>
private void connectoToMySql(string server)
{
//set your connection string.
//NOTE: I am a big supporter of having the connection
//stored in the web.config, not inline like this
string connString = "SERVER=" + server + ";" +
"DATABASE=mydatabase;" +
"UID=testuser;" +
"PASSWORD=testpassword;";
//create your mySQL connection
MySqlConnection cnMySQL = new MySqlConnection(connString);
//create your mySql command object
MySqlCommand cmdMySQL = cnMySQL.CreateCommand();
//create your mySQL reeader object
MySqlDataReader reader;
//set the command text (query) of the
//mySQL command object
cmdMySQL.CommandText = "select * from mycustomers";
//open the mySQL connection
cnMySQL.Open();
//execute the reader, thus retrieving the data
reader = cmdMySQL.ExecuteReader();
//while theres data keep reading
while (reader.Read())
{
string thisrow = "";
for (int i = 0; i < reader.FieldCount; i++)
thisrow += reader.GetValue(i).ToString() + ",";
listBox1.Items.Add(thisrow);
}
cnMySQL.Close();
}
Asp.net c#当前上下文中不存在名称“Encoding”
作者:admin 日期:2009-01-05
javascript实现图片在一定范围内拖放
作者:admin 日期:2009-01-01
以下红色代码放入<head>和</head>之间:
<script type="text/javascript">
<!-- Begin
var ie=document.all;
var nn6=document.getElementById&&!document.all;
var isdrag=false;
var x,y;
var dobj;
function movemouse(e)
{
if (isdrag)
{
var left = nn6 ? tx + e.clientX - x : tx + event.clientX - x;
var top = nn6 ? ty + e.clientY - y : ty + event.clientY - y;
left = left < 0 ? left: 0;
top = top < 0 ? left:0 ;
left = left > photodiv.clientWidth - dobj.clientWidth ? left : photodiv.clientWidth - dobj.clientWidth ;
top = top > photodiv.clientHeight - dobj.clientHeight ? top : photodiv.clientHeight - dobj.clientHeight;
dobj.style.top = top;
dobj.style.left = left;
return false;
}
}
function selectmouse(e)
{
var fobj = nn6 ? e.target : event.srcElement;
var topelement = nn6 ? "HTML" : "BODY";
while (fobj.tagName != topelement && fobj.className != "dragme")
{
fobj = nn6 ? fobj.parentNode : fobj.parentElement;
}
if (fobj.className=="dragme")
{
isdrag = true;
dobj = fobj;
tx = parseInt(dobj.style.left+0);
ty = parseInt(dobj.style.top+0);
x = nn6 ? e.clientX : event.clientX;
y = nn6 ? e.clientY : event.clientY;
document.onmousemove=movemouse;
return false;
}
}
document.onmousedown=selectmouse;
document.onmouseup=new Function("isdrag=false");
// End -->
</script>
广告位