深度优先

这个家伙好懒,除了文章什么都没留下

0%

昨天微信公众号推送一篇【朋友圈访客可以查询了】,虽然猜到了应该是个标题党

里面讲如何查询 自己访客的一种方法,当然只是一种套路而已,并不能真的查询到结果

然后受那个启发,在此基础上做了点改装。

在输入自己微信号和Ta的微信号后,点击访客记录查询

当然是什么结果的查不到的,但是这条查询记录被存储到数据库中了

在界面中留有一个【小程序-后台入口】,但似乎没几个人发现了它

点击下就会看到提示:点击五次进入…点击四次进入…,…

进入就是这样的:

然后往下面滑:

在那个【查询-后台入口】里可以查询到ta查询过的记录

输入1的时候能查询出所有的查询记录,用户的浏览记录被站长统计记录了。

好吧,我也是没什么事做,才写个玩的。还是没有妹子主导,才这么无聊

额,要什么妹子的,自己new一个啊

http://www.cnblogs.com/lindana/archive/2009/03/25/1421345.html

一. Base64的编码规则

Base64编码的思想是是采用64个基本的ASCII码字符对数据进行重新编码。它将需要编码的数据拆分成字节数组。以3个字节为一组。按顺序排列24 位数据,再把这24位数据分成4组,即每组6位。再在每组的的最高位前补两个0凑足一个字节。这样就把一个3字节为一组的数据重新编码成了4个字节。当所要编码的数据的字节数不是3的整倍数,也就是说在分组时最后一组不够3个字节。这时在最后一组填充1到2个0字节。并在最后编码完成后在结尾添加1到2个 “=”。

例:将对ABC进行BASE64编码:

1、首先取ABC对应的ASCII码值。A(65)B(66)C(67);
2、再取二进制值A(01000001)B(01000010)C(01000011);
3、然后把这三个字节的二进制码接起来(010000010100001001000011);
4、 再以6位为单位分成4个数据块,并在最高位填充两个0后形成4个字节的编码后的值,(00010000)(00010100)(00001001)(00000011),其中蓝色部分为真实数据;
5、再把这四个字节数据转化成10进制数得(16)(20)(9)(3);
6、最后根据BASE64给出的64个基本字符表,查出对应的ASCII码字符(Q)(U)(J)(D),这里的值实际就是数据在字符表中的索引。

注:BASE64字符表:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/

二.解码规则

解码过程就是把4个字节再还原成3个字节再根据不同的数据形式把字节数组重新整理成数据。

三. C#中的实现

编码:

byte[] bytes = Encoding.Default.GetBytes(“要转换的字符”);
string str = Convert.ToBase64String(bytes);

解码:

byte[] outputb = Convert.FromBase64String(str);
string orgStr = Encoding.Default.GetString(outputb);

**C#图片的Base64编码和解码 **

图片的Base64编码:

System.IO.MemoryStream m = new System.IO.MemoryStream();
System.Drawing.Bitmap bp = new System.Drawing.Bitmap(@“c:\demo.GIF”);
bp.Save(m, System.Drawing.Imaging.ImageFormat.Gif);
byte[]b= m.GetBuffer();
string base64string=Convert.ToBase64String(b);

Base64字符串解码:

byte[] bt = Convert.FromBase64String(base64string);
System.IO.MemoryStream stream = new System.IO.MemoryStream(bt);
Bitmap bitmap = new Bitmap(stream);
pictureBox1.Image = bitmap;

http://www.weste.net/html/200408/20040803QBI163429.html

把bmp图片转换为jpg图片(C#)

[csharp] view plain copy

using System.IO;
using System.Drawing.Imaging;

public class imgConvert
{
private void BMPToJPG(string bmpFileName,string jpgFileName)
{

System.Drawing.Image img;
img=ReturnPhoto(bmpFileName);

img.Save(jpgFileName,ImageFormat.Jpeg);
}

private Image ReturnPhoto(string bmpFileName)
{
System.IO.FileStream stream ;
stream=File.OpenRead(bmpFileName);
Bitmap bmp = new Bitmap(stream);
System.Drawing.Image image = bmp;//得到原图
//创建指定大小的图
System.Drawing.Image newImage = image.GetThumbnailImage(bmp.Width, bmp.Height,null, new IntPtr());
Graphics g=Graphics.FromImage(newImage);
g.DrawImage(newImage,0,0, newImage.Width, newImage.Height); //将原图画到指定的图上
g.Dispose();
stream.Close();
return newImage;
}
}

http://www.csframework.com/archive/2/arc-2-20110617-1635.htm

PNG ->JPG, BMP -> PNG按比例缩小图片

[csharp] view plain copy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
sing System; 
sing System.Collections.Generic;
sing System.Text;
sing System.IO;
sing System.Drawing;
sing System.Drawing.Imaging;

amespace www.CSFramework.com

public class CImageLibrary
{
public enum ValidateImageResult { OK, InvalidFileSize, InvalidImageSize }

//检查图片大小
public static ValidateImageResult ValidateImage(string file, int MAX_FILE_SIZE, int MAX_WIDTH, int MAX_HEIGHT)
{
byte[] bs = File.ReadAllBytes(file);

double size = (bs.Length / 1024);
//大于50KB
if (size > MAX_FILE_SIZE) return ValidateImageResult.InvalidFileSize;
Image img = Image.FromFile(file);
if (img.Width > MAX_WIDTH || img.Height > MAX_HEIGHT) return ValidateImageResult.InvalidImageSize;
return ValidateImageResult.OK;
}

//按宽度比例缩小图片
public static Image GetOutputSizeImage(Image imgSource, int MAX_WIDTH)
{
Image imgOutput = imgSource;

Size size = new Size(imgSource.Width, imgSource.Height);
if (imgSource.Width <= 3 || imgSource.Height <= 3) return imgSource; //3X3大小的图片不转换

if (imgSource.Width > MAX_WIDTH || imgSource.Height > MAX_WIDTH)
{
double rate = MAX_WIDTH / (double)imgSource.Width;

if (imgSource.Height * rate > MAX_WIDTH)
rate = MAX_WIDTH / (double)imgSource.Height;

size.Width = Convert.ToInt32(imgSource.Width * rate);
size.Height = Convert.ToInt32(imgSource.Height * rate);

imgOutput = imgSource.GetThumbnailImage(size.Width, size.Height, null, IntPtr.Zero);
}

return imgOutput;
}

//按比例缩小图片
public static Image GetOutputSizeImage(Image imgSource, Size outSize)
{
Image imgOutput = imgSource.GetThumbnailImage(outSize.Width, outSize.Height, null, IntPtr.Zero);
return imgOutput;
}

/// <summary>
/// 由图片文件转字节
/// </summary>
public static byte[] GetImageBytes(string imageFileName)
{
Image img = Image.FromFile(imageFileName);
return GetImageBytes(img);
}

/// <summary>
/// 图片转字节
/// </summary>
public static byte[] GetImageBytes(Image img)
{
if (img == null) return null;
try
{
System.IO.MemoryStream ms = new MemoryStream();
img.Save(ms, ImageFormat.Jpeg);
byte[] bs = ms.ToArray();
ms.Close();
return bs;
}
catch { return null; }
}

/// <summary>
/// 字节转图片
/// </summary>
public static Image FromBytes(byte[] bs)
{
if (bs == null) return null;
try
{
MemoryStream ms = new MemoryStream(bs);
Image returnImage = Image.FromStream(ms);
ms.Close();
return returnImage;
}
catch { return null; }
}

/// <summary>
/// 将其它格式的图片转为JPG文件
/// </summary>
public static Image ToJPG(Image source)
{
//注意,先定义Bitmap类,否则会报A generic error occurred in GDI+
Bitmap bmp = new Bitmap(source);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, ImageFormat.Jpeg);
return Bitmap.FromStream(ms);
}

/// <summary>
/// 将其它格式的图片转为PNG文件
/// </summary>
public static Image ToPNG(Image source)
{
//注意,先定义Bitmap类,否则会报A generic error occurred in GDI+
Bitmap bmp = new Bitmap(source);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, ImageFormat.Png);
return FromBytes(ms.ToArray());
}

//保存文件
public static void SaveFile(string fileName, Image source)
{
source.Save(fileName, source.RawFormat);
}

}
}

在调用阿里云上面的api接口时,有些图片处理接口都需要将图片文件转换成base64,传递参数然后才能调用。

网上搜了下 一些图片和base转换的方法:

一、通过jQuery方法转换

html代码部分:

1
2
3
<input type="file" name="file" id="img_upload_file" value="" multiple="multiple" />
<input type="hidden" name="img_upload_base" id="img_upload_base" />
<textarea rows="30" cols="100" id="img_upload_base_text"></textarea>

js代码部分 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<script src="scripts/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(function () {
$("#img_upload_file").change(function () {
var file = this.files[0];
var reader = new FileReader();
reader.readAsDataURL(file);//调用自带方法进行转换
reader.onload = function (e) {
$("#img_upload_show").attr("src", this.result);//将转换后的编码存入src完成预览
$("#img_upload_base").val(this.result);//将转换后的编码保存到input供后台使用
$("#img_upload_base_text").val(this.result);//显示出 转换后的编码数据
};
});
});
</script>

这个方法比较简单,省时省力,转换的操作都放到用户浏览器上进行,减少服务器压力。

二 、asp后台处理转换

html代码部分:

1
2
3
<asp:FileUpload ID="file_upload" runat="server" />
<asp:Button ID="btn" runat="server" Text="后台转换" OnClick="btn_Click" />
<img id="img_upload_show" src="" />

aps后台代码部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
protected void btn_Click(object sender, EventArgs e)
{
//获取上传 文件的名称
string btn_file= file_upload.PostedFile.FileName;
//获取 服务器上路径
string imgPath = Server.MapPath("Imagers/");
//获取 文件后缀
string houzui = btn_file.Substring(btn_file.LastIndexOf('.'));
//上传文件 路径 ,重命名
string filePath = imgPath + Guid.NewGuid().ToString()+ houzui;
//上传文件
file_upload.SaveAs(filePath);

//转换base64
string result = imgToBase64(filePath);

//写入txt文件中
string path = Server.MapPath("/Base");
System.IO.File.WriteAllText(path + "//" + Guid.NewGuid().ToString() + ".txt", result);
}

imagerToBase64的方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/// <summary>
/// imager 转换 成Base64
/// </summary>
/// <param name="filePath">文件路径</param>
/// <returns>base64编码</returns>
private string imgToBase64(string filePath)
{
//用streamread读这个文件
System.IO.StreamReader sr = new StreamReader(filePath, Encoding.Default, true);

int index;
//实例化一个内存流
System.IO.MemoryStream tempStream = new MemoryStream();
//将流转换为字节数组
while ((index = sr.BaseStream.ReadByte()) != -1)
{
tempStream.WriteByte(((byte)index));
}
byte[] array = tempStream.ToArray();
tempStream.Close();
//将得到的字节数组转换为base64位编码
string result = Convert.ToBase64String(array);
return result;
}

这个方法不太方便,需要先将图片上传至服务器,然后得到图片的路径,再进行 转换。

三、Base64编码转图片

在写这个时候,遇到个bug,Bitmap参数无效,原来是错误的将 gif 的 动态图的编码转成Bitmap所以会报错的 额 。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/// <summary>
/// base64编码的文本 转为 图片
/// </summary>
/// <param name="txtFileName">Base编码的txt文本路径 </param>
private void Base64StringToImage(string txtFileName)
{
try
{
FileStream ifs = new FileStream(txtFileName, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(ifs);
//读取txt里面的内容
String inputStr = sr.ReadToEnd();

//转图片
byte[] bt = Convert.FromBase64String(inputStr);
System.IO.MemoryStream stream = new System.IO.MemoryStream(bt);
Bitmap bmp = new Bitmap(stream);

string fileName = txtFileName.Substring(0, txtFileName.IndexOf("."));

if (File.Exists(fileName))
{
File.Delete(fileName);
}
//存储到服务器 上
bmp.Save(fileName + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
bmp.Save(fileName + ".bmp", ImageFormat.Bmp);
bmp.Save(fileName + ".gif", ImageFormat.Gif);
bmp.Save(fileName + ".png", ImageFormat.Png);
stream.Close();
sr.Close();
ifs.Close();
}
catch (Exception ex)
{
//("Base64StringToImage 转换失败\nException:" + ex.Message);
}
}