//提供一段代码,供LZ参考
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
using System.Windows.Forms;namespace LandaControl8_sqlserverImage
{
/// <summary>
/// 对图像进行一系列处理
/// </summary>
public class TreatImage
{
#region Bitmap转换byte[]数组
/// <summary>
/// Bitmap转换byte[]数组
/// </summary>
/// <param name="bmp"></param>
/// <returns></returns>
public static byte[] Bmptobyte(Bitmap bmp)
{
MemoryStream ms = new MemoryStream();
bmp.Save(ms, ImageFormat.Jpeg);
ms.Flush();
byte[] buffer = ms.GetBuffer();
ms.Close();
return buffer;
}
#endregion #region byte[]数组转换Bitmap
/// <summary>
/// byte[]数组转换Bitmap
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static Bitmap bytetobmp(byte[] buffer)
{
MemoryStream ms = new MemoryStream();
ms.Write(buffer, 0, buffer.Length);
Bitmap bmp = new Bitmap(ms);
ms.Close();
return bmp;
}
#endregion #region 选取本地图片
/// <summary>
/// 选取本地图片
/// </summary>
/// <param name="IMG"></param>
/// <returns></returns>
public static Bitmap LocalIMG(string IMG)
{
FileStream fs = new FileStream(IMG, FileMode.Open);
Bitmap bmp = new Bitmap(fs);
fs.Close();
return bmp;
}
#endregion #region 返回流状态图片
/// <summary>
/// 返回流状态图片
/// </summary>
/// <param name="Img"></param>
/// <returns></returns>
public static Bitmap ImgFromBase64(string Img)
{
Bitmap bmp;
byte[] buffer = Convert.FromBase64String(Img);
if (buffer.Length > 0)
{
MemoryStream ms = new MemoryStream();
ms.Write(buffer, 0, buffer.Length);
bmp = new Bitmap(ms);
ms.Close();
return bmp;
}
else
{
bmp = DefaultPic();
return bmp;
}
}
#endregion #region 默认图片
/// <summary>
/// 默认图片
/// </summary>
/// <returns></returns>
public static Bitmap DefaultPic()
{
FileStream fs = new FileStream(Application.StartupPath + @"\Goodr.jpg", FileMode.Open);
Bitmap bmp = new Bitmap(fs);
fs.Close();
return bmp;
}
#endregion private void numericUpDown2_ValueChanged(object sender, System.EventArgs e)
{//更新图像显示
//this.button2.Refresh();
}
#endregion #region 尺寸枚举
public enum Dimensions
{
Width,
Height
}
#endregion #region 锚位置枚举
public enum AnchorPosition
{
Top,
Center,
Bottom,
Left,
Right
}
#endregion #region 按比例转换图像
/// <summary>
/// 返回按比价转换后的图像
/// </summary>
/// <param name="imgPhoto">指定图像</param>
/// <param name="Size">指定尺寸</param>
/// <param name="Dimension">按宽度或高度</param>
/// <returns>返回按比价转换后的图像</returns>
public static Image ConstrainProportions(Image imgPhoto, int Size, Dimensions Dimension)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
float nPercent = 0; switch (Dimension)
{
case Dimensions.Width:
nPercent = ((float)Size / (float)sourceWidth);
break;
default:
nPercent = ((float)Size / (float)sourceHeight);
break;
} int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent); Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel); grPhoto.Dispose();
return bmPhoto;
}
#endregion #region 按固定尺寸转换图像
/// <summary>
/// 返回按固定尺寸转换的图像
/// </summary>
/// <param name="imgPhoto">指定图像</param>
/// <param name="Width">生成后图像的宽度</param>
/// <param name="Height">生成后图像的高度</param>
/// <returns>返回按固定尺寸转换的图像</returns>
public static Image FixedSize(Image imgPhoto, int Width, int Height)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0; float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0; nPercentW = ((float)Width / (float)sourceWidth);
nPercentH = ((float)Height / (float)sourceHeight); //if we have to pad the height pad both the top and the bottom
//with the difference between the scaled height and the desired height
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = (int)((Width - (sourceWidth * nPercent)) / 2);
}
else
{
nPercent = nPercentW;
destY = (int)((Height - (sourceHeight * nPercent)) / 2);
} int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent); Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.Red);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel); grPhoto.Dispose();
return bmPhoto;
}
#endregion }
}
Bitmap JTxt = JT.Clone(xtrect, JT.PixelFormat ); 试试.