发送短信到网站 http://www.smschinese.cn/ 上注册用户获取到(要填签名) THE_UID和THE_KEY。smsMob:手机号,smsText发送的信息(两个参数不能为空)。先看效果图:
短信代码:
string THE_UID = "账户id";
string THE_KEY = "密钥";
//smsMob手机号 smsText发送的信息
public string GetPostUrl(string smsMob, string smsText)
{
string postUrl = "http://utf8.api.smschinese.cn/?Uid=" + THE_UID + "&key=" + THE_KEY + "&smsMob=" + smsMob + "&smsText=" + smsText;
return postUrl;
}
//发送短信
public string PostSmsInfo(string url)
{
string strRet = null;
if (url.Trim().ToString() == "" || url == null) {
return strRet;
}
try
{
HttpWebRequest hr = (HttpWebRequest)WebRequest.Create(url);
hr.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
hr.Method = "GET";
hr.Timeout = 30 * 60 * 1000;
WebResponse hs = hr.GetResponse();
Stream sr = hs.GetResponseStream();
StreamReader ser = new StreamReader(sr, Encoding.Default);
strRet = ser.ReadToEnd();
}
catch (Exception)
{
strRet = null;
}
return strRet;
}
//发送短信返回的结果
public string GetResult(string strRet)
{
int result = 0;
try
{
result = int.Parse(strRet);
switch (result)
{
case -1:
strRet = "没有该用户账户";
break;
case -2:
strRet = "接口密钥不正确,不是账户登陆密码";
break;
case -21:
strRet = "MD5接口密钥加密不正确";
break;
case -3:
strRet = "短信数量不足";
break;
case -11:
strRet = "该用户被禁用";
break;
case -14:
strRet = "短信内容出现非法字符";
break;
case -4:
strRet = "手机号格式不正确";
break;
case -41:
strRet = "手机号码为空";
break;
case -42:
strRet = "短信内容为空";
break;
case -51:
strRet = "短信签名格式不正确,接口签名格式为:【签名内容】";
break;
case -6:
strRet = "IP限制";
break;
default:
strRet = "发送短信数量:" + result;
break;
}
}
catch (Exception ex)
{
strRet = ex.Message;
}
return strRet;
}
生成没有logo的二维码:
Image QRCode = null;
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
qrCodeEncoder.QRCodeScale = 4;
qrCodeEncoder.QRCodeVersion = 10;//自定义
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
string url = GetPostUrl(this.textBox1.Text.Trim(), this.textBox2.Text.Trim());
try
{
QRCode = qrCodeEncoder.Encode("http://www.baidu.com", Encoding.UTF8);//生成二维码
}
catch
{
}
this.pictureBox1.Image = (Image)QRCode;
this.pictureBox1.Image.Save(Application.StartupPath + "\\" + DateTime.Now.ToFileTime().ToString() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);//保存生成二维码的图片
生成有logo的二维码:
Image QRCode = null;
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
qrCodeEncoder.QRCodeScale = 4;
qrCodeEncoder.QRCodeVersion = 10;//自定义
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
string url = GetPostUrl(this.textBox1.Text.Trim(), this.textBox2.Text.Trim());
try
{
QRCode = qrCodeEncoder.Encode(url, Encoding.UTF8);//生成二维码
}
catch
{
}
Image img = this.pictureBox1.Image;
this.pictureBox1.Image = (Image)QRCode;
ImageUtility IU = new ImageUtility();
Image qRImage = (Image)IU.MergeQrImg((Bitmap)QRCode, (Bitmap)img);
this.pictureBox1.Image = qRImage;//生成有logo的二维码
this.pictureBox1.Image.Save(Application.StartupPath + "\\" + DateTime.Now.ToFileTime().ToString() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);//保存有logo的二维码
解析图片的二维码:没有logo的二维码解析的没问题,有logo的二维码解析好像乱码但在微信上解析没问题(烦 --_--),微信识别二维码后会发送短信。
try{
QRCodeDecoder qrCodeDecoder = new QRCodeDecoder();
this.textBox3.Text = qrCodeDecoder.decode((new QRCodeBitmapImage(new Bitmap(this.pictureBox2.Image))), Encoding.UTF8);//解析二维码
}catch{
}
ImageUtility类:
public class ImageUtility
{
#region 合并用户QR图片和用户头像
/// <summary>
/// 合并用户QR图片和用户头像
/// </summary>
/// <param name="qrImg">QR图片</param>
/// <param name="headerImg">用户头像</param>
/// <param name="n"></param>
/// <returns></returns>
public Bitmap MergeQrImg(Bitmap qrImg, Bitmap headerImg, double n = 0.23)
{
int margin = 10;
float dpix = qrImg.HorizontalResolution;
float dpiy = qrImg.VerticalResolution;
var _newWidth = (10 * qrImg.Width - 36 * margin) * 1.0f / 36;
var _headerImg = ZoomPic(headerImg, _newWidth / headerImg.Width);
//处理头像
int newImgWidth = _headerImg.Width + margin;
Bitmap headerBgImg = new Bitmap(newImgWidth, newImgWidth);
headerBgImg.MakeTransparent();
Graphics g = Graphics.FromImage(headerBgImg);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.Transparent);
Pen p = new Pen(new SolidBrush(Color.White));
Rectangle rect = new Rectangle(0, 0, newImgWidth - 1, newImgWidth - 1);
using (GraphicsPath path = CreateRoundedRectanglePath(rect, 1))
{
g.DrawPath(p, path);
g.FillPath(new SolidBrush(Color.White), path);
}
//画头像
Bitmap img1 = new Bitmap(_headerImg.Width, _headerImg.Width);
Graphics g1 = Graphics.FromImage(img1);
g1.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g1.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g1.Clear(Color.Transparent);
Pen p1 = new Pen(new SolidBrush(Color.Gray));
Rectangle rect1 = new Rectangle(0, 0, _headerImg.Width - 1, _headerImg.Width - 1);
using (GraphicsPath path1 = CreateRoundedRectanglePath(rect1, 1))
{
g1.DrawPath(p1, path1);
TextureBrush brush = new TextureBrush(_headerImg);
g1.FillPath(brush, path1);
}
g1.Dispose();
PointF center = new PointF((newImgWidth - _headerImg.Width) / 2, (newImgWidth - _headerImg.Height) / 2);
g.DrawImage(img1, center.X, center.Y, _headerImg.Width, _headerImg.Height);
g.Dispose();
Bitmap backgroudImg = new Bitmap(qrImg.Width, qrImg.Height);
backgroudImg.MakeTransparent();
backgroudImg.SetResolution(dpix, dpiy);
headerBgImg.SetResolution(dpix, dpiy);
Graphics g2 = Graphics.FromImage(backgroudImg);
g2.Clear(Color.Transparent);
g2.DrawImage(qrImg, 0, 0);
PointF center2 = new PointF((qrImg.Width - headerBgImg.Width) / 2, (qrImg.Height - headerBgImg.Height) / 2);
g2.DrawImage(headerBgImg, center2);
g2.Dispose();
return backgroudImg;
}
#endregion
#region 图形处理
/// <summary>
/// 创建圆角矩形
/// </summary>
/// <param name="rect">区域</param>
/// <param name="cornerRadius">圆角角度</param>
/// <returns></returns>
private GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int cornerRadius)
{
//圆角矩形
GraphicsPath roundedRect = new GraphicsPath();
roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
roundedRect.CloseFigure();
return roundedRect;
}
/// <summary>
/// 图片按比例缩放
/// </summary>
private Image ZoomPic(Image initImage, double n)
{
//缩略图宽、高计算
double newWidth = initImage.Width;
double newHeight = initImage.Height;
newWidth = n * initImage.Width;
newHeight = n * initImage.Height;
//生成新图
//新建一个bmp图片
System.Drawing.Image newImage = new System.Drawing.Bitmap((int)newWidth, (int)newHeight);
//新建一个画板
System.Drawing.Graphics newG = System.Drawing.Graphics.FromImage(newImage);
//设置质量
newG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
newG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//置背景色
newG.Clear(Color.Transparent);
//画图
newG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, newImage.Width, newImage.Height), new System.Drawing.Rectangle(0, 0, initImage.Width, initImage.Height), System.Drawing.GraphicsUnit.Pixel);
newG.Dispose();
return newImage;
}
/// <summary>
/// 创建缩略图
/// </summary>
/// <param name="b"></param>
/// <param name="destHeight"></param>
/// <param name="destWidth"></param>
/// <returns></returns>
public static Bitmap GetThumbnail(Bitmap b, int destHeight, int destWidth)
{
System.Drawing.Image imgSource = b;
System.Drawing.Imaging.ImageFormat thisFormat = imgSource.RawFormat;
int sW = 0, sH = 0;
// 按比例缩放
int sWidth = imgSource.Width;
int sHeight = imgSource.Height;
if (sHeight > destHeight || sWidth > destWidth)
{
if ((sWidth * destHeight) > (sHeight * destWidth))
{
sW = destWidth;
sH = (destWidth * sHeight) / sWidth;
}
else
{
sH = destHeight;
sW = (sWidth * destHeight) / sHeight;
}
}
else
{
sW = sWidth;
sH = sHeight;
}
Bitmap outBmp = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage(outBmp);
g.Clear(Color.Transparent);
// 设置画布的描绘质量
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(imgSource, new Rectangle((destWidth - sW) / 2, (destHeight - sH) / 2, sW, sH), 0, 0, imgSource.Width, imgSource.Height, GraphicsUnit.Pixel);
g.Dispose();
// 以下代码为保存图片时,设置压缩质量
EncoderParameters encoderParams = new EncoderParameters();
long[] quality = new long[1];
quality[0] = 100;
EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
imgSource.Dispose();
return outBmp;
}
#endregion
}