实现效果:
1.图片修改前:
2.图片编辑时:
3.图片修改后:
PictrueEdit 控件初始化代码:
void SetPictrueEdit()
{
txt图片.Properties.AllowScrollViaMouseDrag = true;
txt图片.Properties.AllowZoomOnMouseWheel = DevExpress.Utils.DefaultBoolean.True;
txt图片.Properties.Appearance.Options.UseTextOptions = true;
txt图片.Properties.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
txt图片.Properties.Appearance.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
txt图片.Properties.ContextButtonOptions.BottomPanelColor = System.Drawing.Color.FromArgb(((int)(((byte)(160)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
txt图片.Properties.ContextButtonOptions.Indent = 3;
txt图片.Properties.ContextButtonOptions.TopPanelColor = System.Drawing.Color.FromArgb(((int)(((byte)(160)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
contextButton6.Alignment = DevExpress.Utils.ContextItemAlignment.BottomFar;
contextButton6.AllowGlyphSkinning = DevExpress.Utils.DefaultBoolean.True;
contextButton6.AppearanceHover.ForeColor = System.Drawing.Color.White;
contextButton6.AppearanceHover.Options.UseForeColor = true;
contextButton6.AppearanceNormal.ForeColor = System.Drawing.Color.White;
contextButton6.AppearanceNormal.Options.UseForeColor = true;
contextButton6.Glyph = m_imgContextButtonClose;
contextButton6.Id = new System.Guid("14a76b7c-b704-4644-b1b7-fe833ae4acc5");
contextButton6.Name = "itemRemove";
contextButton7.Alignment = DevExpress.Utils.ContextItemAlignment.BottomNear;
contextButton7.AllowGlyphSkinning = DevExpress.Utils.DefaultBoolean.True;
contextButton7.AppearanceHover.ForeColor = System.Drawing.Color.White;
contextButton7.AppearanceHover.Options.UseForeColor = true;
contextButton7.AppearanceNormal.ForeColor = System.Drawing.Color.White;
contextButton7.AppearanceNormal.Options.UseForeColor = true;
contextButton7.Glyph = m_imgcontextButtonDownLoad;
contextButton7.Id = new System.Guid("7346c2ed-8a36-4c16-bfc0-c8599f20a63d");
contextButton7.Name = "itemDownload";
trackBarContextButton1.Alignment = DevExpress.Utils.ContextItemAlignment.MiddleBottom;
trackBarContextButton1.AllowUseMiddleValue = true;
trackBarContextButton1.Id = new System.Guid("04d18c6d-6c39-4bed-81a5-fee056e3aad9");
trackBarContextButton1.Maximum = 500;
trackBarContextButton1.Middle = 100;
trackBarContextButton1.Name = "TrackBarContextButton";
trackBarContextButton1.ShowZoomButtons = false;
txt图片.Properties.ContextButtons.Add(contextButton6);
txt图片.Properties.ContextButtons.Add(contextButton7);
txt图片.Properties.ContextButtons.Add(trackBarContextButton1);
txt图片.Properties.NullText = "没有图像";
txt图片.Properties.ZoomingOperationMode = DevExpress.XtraEditors.Repository.ZoomingOperationMode.MouseWheel;
txt图片.Properties.ContextButtonClick += new DevExpress.Utils.ContextItemClickEventHandler(txt图片_Properties_ContextButtonClick);
txt图片.Properties.ContextButtonValueChanged += new DevExpress.Utils.ContextButtonValueChangedEventHandler(txt图片_Properties_ContextButtonValueChanged);
txt图片.Size = new System.Drawing.Size(366, 244);
txt图片.TabIndex = 0;
txt图片.ZoomPercentChanged += new System.EventHandler(txt图片_ZoomPercentChanged);
}
private void txt图片_Properties_ContextButtonValueChanged(object sender, ContextButtonValueEventArgs e)
{
if (e.Item.Name == "TrackBarContextButton")
{
txt图片.Properties.ZoomPercent = Convert.ToDouble(e.Value);
}
}
private void txt图片_Properties_ContextButtonClick(object sender, ContextItemClickEventArgs e)
{
ContextItemClick(e);
}
void ContextItemClick(ContextItemClickEventArgs e)
{
if (e.Item.Name == "itemDownload")
{
Image img = ImportImage();
if (img != null)
{
txt图片.Image = img;
}
}
else if (e.Item.Name == "itemRemove")
{
txt图片.Image = null;
}
}
private void txt图片_ZoomPercentChanged(object sender, EventArgs e)
{
(txt图片.Properties.ContextButtons["TrackBarContextButton"] as TrackBarContextButton).Value = Convert.ToInt32(txt图片.Properties.ZoomPercent);
}
获取编辑后的图片:
using (var catchBmp = new Bitmap(txt图片.Width, txt图片.Height))
{
using (var g = Graphics.FromImage(catchBmp))
{
Bitmap img = (Bitmap)txt图片.Image;
int wi = Convert.ToInt32(txt图片.Properties.ZoomPercent * img.Width / 100);
int hi = Convert.ToInt32(txt图片.Properties.ZoomPercent * img.Height / 100);
Bitmap bi = KiResizeImage(img, wi, hi);
PictureEditViewInfo viewInfo = txt图片.GetViewInfo() as PictureEditViewInfo;
txt图片.Image = KiCut(bi, Convert.ToInt32(viewInfo.PicturePosition.X),
Convert.ToInt32(viewInfo.PicturePosition.Y), txt图片.Width, txt图片.Height);
(txt图片.Properties.ContextButtons["TrackBarContextButton"] as TrackBarContextButton).Value = 100;
Image 图片 = GetByteImage(txt图片.Image);
}
}
//将image转化为二进制
public static byte[] GetByteImage(Image img)
{
byte[] bt = null;
if (img!=null)
{
using (MemoryStream mostream = new MemoryStream())
{
Bitmap bmp = new Bitmap(img);
bmp.Save(mostream, System.Drawing.Imaging.ImageFormat.Jpeg);//将图像以指定的格式存入缓存内存流
bt = new byte[mostream.Length];
mostream.Position = 0;//设置留的初始位置
mostream.Read(bt, 0, Convert.ToInt32(bt.Length));
}
}
return bt;
}
图像处理函数:
private ImageCodecInfo GetEncoder(ImageFormat format)//获取特定的图像编解码信息
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
//对图片进行缩放
public Image GetReducedImage(Image img)
{
System.Drawing.Imaging.Encoder encoder = System.Drawing.Imaging.Encoder.Quality;//获取品质(压缩率)编码
EncoderParameter mycoder = new EncoderParameter(encoder, 30L);//0压缩率最大,100品质最高,此处输入30L最适合
EncoderParameters myCoders = new EncoderParameters(1);//参数数组,大小为1
myCoders.Param[0] = mycoder;//添加一个参数
ImageCodecInfo jpgInfo = GetEncoder(ImageFormat.Jpeg);//获取JPG格式编解码信息
Bitmap bmp = KiResizeImage((Bitmap)img, img.Width / 5, img.Height / 5); //设置为了原图片的五分之一进行缩放,测试原图片 50KB,执行后2KB
return bmp;
}
///
/// Resize图片
///
/// 原始Bitmap
/// 新的宽度
/// 新的高度
/// 保留着,暂时未用
/// 处理以后的图片
public static Bitmap KiResizeImage(Bitmap bmp, int newW, int newH)
{
try
{
Bitmap b = new Bitmap(newW, newH);
Graphics g = Graphics.FromImage(b);
// 插值算法的质量
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
g.Dispose();
return b;
}
catch
{
return null;
}
}
///
/// 剪裁 -- 用GDI+
///
/// 原始Bitmap
/// 开始坐标X
/// 开始坐标Y
/// 宽度
/// 高度
/// 剪裁后的Bitmap
public static Bitmap KiCut(Bitmap b, int StartX, int StartY, int iWidth, int iHeight)
{
if (b == null)
{
return null;
}
int w = b.Width;
int h = b.Height;
if (StartX >= w || StartY >= h)
{
return null;
}
if (StartX + iWidth > w)
{
iWidth = w - StartX;
}
if (StartY + iHeight > h)
{
iHeight = h - StartY;
}
try
{
Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bmpOut);
g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);
g.Dispose();
return bmpOut;
}
catch
{
return null;
}
}
上面代码是网上搜索并结合自己的想法而写,如果对代码有问题的可以留言