-
2019-07-19 19:21:01
/// <summary> /// 改变图像大小 /// </summary> public static Image ReSizeImage(Image img, Size size) { Bitmap bitmap = new Bitmap(size.Width, size.Height); Graphics g = Graphics.FromImage(bitmap); g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(img, 0, 0, bitmap.Width, bitmap.Height); g.Dispose(); return bitmap; }
更多相关内容 -
C# winform 图片编辑器
2020-02-05 06:10:16C# winform 图片编辑器 实现本地载入 缩放 按比例缩放功能 C# winform 图片编辑器 实现本地载入 缩放 按比例缩放功能 -
C# winform 图片编辑源代码
2022-03-08 10:40:091.画笔,画板,画色 2.撤回 3.保存 -
C#简易图片编辑器 vs2010
2017-05-18 20:20:52C#简易图片编辑器 vs2010 -
picface:使用 C# 和 exiftool 编辑图片元数据
2021-06-14 17:53:12如果您在 Picasa 中使用面部功能,您的所有信息都存储在 .picasa.ini 文件中,而不是您的图片中。 此工具读取 .picasa.ini 文件并使用 XMP 将数据写入您的图片... PicFace 是用 C# 和 .NET 3.5 (Visual Studio 2008) 编 -
c#图片编辑软件 有简单的图片编辑功能
2010-05-29 14:44:38c#c#图片编辑软件 有简单的图片编辑功能图片编辑软件 有简单的图片编辑功能 -
c#扫描图片去黑边(扫描仪去黑边)
2020-12-31 07:36:54自动去除图像扫描黑边 代码如下:/// /// 自动去除图像扫描黑边 /// /// ”fileName”> public static void AutoCutBlackEdge(string fileName) { //打开图像 Bitmap bmp = OpenImage(fileName);... -
VS2012写的WinForm版图片查看编辑器
2015-09-28 22:07:42用C#2015 .Net4.5写的图片浏览器,支持png、jpg、bmp、gif的图片,功能有查看缩略图,显示上一张、下一张、图片放大与缩小、垂直翻转、水平翻转,旋转90、180、270度,关联图片格式(即双击图片会以该软件打开),... -
C#下listview如何插入图片
2021-01-01 16:05:15如何在listview中插入图片,相信大家很想知道,...第四步:编辑ListView控件项的ImageIndex行为你就会发现图片成功显示出来了! 附:在ListView控件中添加选项的代码 private void button1_Click(object sender, Ev -
c#在图片上添加文字和二维码源码
2019-06-04 11:06:47c#在图片上添加文字和二维码源码 原文地址:https://blog.csdn.net/u012577474 添加图片方法: DrawImage(Image, 画图位置x, 画图位置y, 图片宽, 图片高); 添加文字方法: DrawString(文字, 字体, 画笔, 位置); -
C#图片编辑并上传(仿新浪微博)
2013-03-04 10:09:56图片上传 裁剪 swfupload Jcrop C#,结合了swfupload 和 Jcrop ,比之前的图片上传更为强大了,多了Jcrop 的裁剪功能,Jcrop目前不支持IE10哦 ,不过不影响上传,仿新浪微博哦,裁剪大小可以通过css来控制哦 -
C#实现图片裁剪、旋转、保存功能
2021-05-18 15:03:44C# 图片裁剪 之前用C#做了一个简单的图片裁剪项目,记录一下。 实现功能 实现点击按钮,选择一张图片,打开、显示; 点击按钮,用鼠标拖动画一个矩形框; 点击按钮,将矩形框内的图片裁剪并保存成一副新的图片. ...C# 图片裁剪
之前用C#做了一个简单的图片裁剪项目,记录一下。
实现功能
- 实现点击按钮,选择一张图片,打开、显示;
- 点击按钮,用鼠标拖动画一个矩形框;
- 点击按钮,将矩形框内的图片裁剪并保存成一副新的图片.
- 支持图片角度调整
运行界面效果
winForm控件
注意还有图中左下角的两个控件
引用和定义:using System; using System.ComponentModel; using System.Drawing; using System.Drawing.Imaging; using System.Reflection; using System.Windows.Forms; using System.IO; using System.Drawing.Drawing2D; namespace xxxx { public partial class Form1 : Form { bool blnDraw; Point start; //画框的起始点 Rectangle rect; Point Pstart; //原图的起始点 Rectangle Orect;//在原图中尺寸 private string filePath = string.Empty; public Form1() { InitializeComponent(); } ..... } }
Windows 窗体的 OpenFileDialog(打开文件对话框)组件是一个预先配置的对话框。它与 Windows 操作系统的“打开文件”对话框相同。该控件是从 CommonDialog 类继承的。在基于 Windows 的应用程序中,可该组件实现简单的文件选择,而不必配置自己的对话框。利用标准的 Windows 对话框,可以创建用户所熟悉的应用程序界面。
Filter 属性:获取或设置当前文件名过滤字符串,该字符串决定对话框的“文件类型”框中出现的选择内容。对于每个过滤选项,过滤字符串都包含过滤条件说明,后接一垂直线条(|)和过滤模式。不同过滤选项的字符串由垂直线条隔开。
例如:“Text files (.txt)|.txt|All files (.)|.”。如果要将多个过滤模式添加到过滤条件中,可用分号将文件类型分隔开,例如:
"Image Files(.BMP;.JPG;.GIF)|.BMP;.JPG;.GIF|All files (.)|."使用 FilterIndex 属性设置第一个显示给用户的过滤选项。
OpenFile() 方法:打开用户选定的具有只读权限的文件,该文件由 FileName 属性指定。
使用该方法可从对话框以只读方式快速打开文件。“打开图片”(button1)按钮绑定事件:
private void button1_Click(object sender, EventArgs e) { openFileDialog1.Filter = "@.Jpg|*.jpg|@.Png|*.png|@.Gif|*.gif|@.All files|*.*"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { FileStream fs = new FileStream(filePath = openFileDialog1.FileName, FileMode.Open, FileAccess.Read); //通过调用系统的画笔工具,画出一个Image类型的数据,传给pictureBox。 Image im = System.Drawing.Bitmap.FromStream(fs); PicSource.Image = im; this.PicSource.SizeMode = PictureBoxSizeMode.Zoom; } }
记录鼠标点击事件,绘制红色矩形框
#region 记录鼠标点击事件 private void PicSource_MouseDown(object sender, MouseEventArgs e) { int originalWidth = this.PicSource.Image.Width; int originalHeight = this.PicSource.Image.Height; PropertyInfo rectangleProperty = this.PicSource.GetType().GetProperty("ImageRectangle", BindingFlags.Instance | BindingFlags.NonPublic); Rectangle rectangle = (Rectangle)rectangleProperty.GetValue(this.PicSource, null); int currentWidth = rectangle.Width; int currentHeight = rectangle.Height; double rate = (double)currentHeight / (double)originalHeight; int black_left_width = (currentWidth == this.PicSource.Width) ? 0 : (this.PicSource.Width - currentWidth) / 2; int black_top_height = (currentHeight == this.PicSource.Height) ? 0 : (this.PicSource.Height - currentHeight) / 2; int zoom_x = e.X - black_left_width; int zoom_y = e.Y - black_top_height; double original_x = (double)zoom_x / rate; double original_y = (double)zoom_y / rate; Pstart = new Point((int)original_x, (int)original_y); start = e.Location; Invalidate(); blnDraw = true; } private void PicSource_MouseMove(object sender, MouseEventArgs e) { if (blnDraw) { if (e.Button != MouseButtons.Left)//判断是否按下左键 return; Point tempEndPoint = e.Location; //记录框的位置和大小 rect.Location = new Point( Math.Min(start.X, tempEndPoint.X), Math.Min(start.Y, tempEndPoint.Y)); rect.Size = new Size( Math.Abs(start.X - tempEndPoint.X), Math.Abs(start.Y - tempEndPoint.Y)); //记录绘制大小 int originalWidth = this.PicSource.Image.Width; int originalHeight = this.PicSource.Image.Height; PropertyInfo rectangleProperty = this.PicSource.GetType().GetProperty("ImageRectangle", BindingFlags.Instance | BindingFlags.NonPublic); Rectangle rectangle = (Rectangle)rectangleProperty.GetValue(this.PicSource, null); int currentWidth = rectangle.Width; int currentHeight = rectangle.Height; double rate = (double)currentHeight / (double)originalHeight; int black_left_width = (currentWidth == this.PicSource.Width) ? 0 : (this.PicSource.Width - currentWidth) / 2; int black_top_height = (currentHeight == this.PicSource.Height) ? 0 : (this.PicSource.Height - currentHeight) / 2; int zoom_x = e.X - black_left_width; int zoom_y = e.Y - black_top_height; double original_x = (double)zoom_x / rate; double original_y = (double)zoom_y / rate; Orect.Location = new Point( Math.Min(Pstart.X, (int)original_x), Math.Min(Pstart.Y, (int)original_y)); Orect.Size = new Size( Math.Abs(Pstart.X - (int)original_x), Math.Abs(Pstart.Y - (int)original_y)); PicSource.Invalidate(); } } private void PicSource_MouseUp(object sender, MouseEventArgs e) { blnDraw = false; //结束绘制 Bitmap bitmap = PartDraw(PicSource.Image, Orect); this.pictureBox1.Image = bitmap; this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; } private void PicSource_Paint(object sender, PaintEventArgs e) { if (blnDraw) { if (PicSource.Image != null) { if (rect != null && rect.Width > 0 && rect.Height > 0) { e.Graphics.DrawRectangle(new Pen(Color.Red, 3), rect);//重新绘制颜色为红色 } } } } #endregion
旋转
注意!!!
button2_click_1绑定的是button2按钮
public static Bitmap KiRotate(Bitmap bmp, float angle, Color bkColor) { int w = bmp.Width + 2; int h = bmp.Height + 2; PixelFormat pf; if (bkColor == Color.Transparent) { pf = PixelFormat.Format32bppArgb; } else { pf = bmp.PixelFormat; } Bitmap tmp = new Bitmap(w, h, pf); Graphics g = Graphics.FromImage(tmp); g.Clear(bkColor); g.DrawImageUnscaled(bmp, 1, 1); g.Dispose(); GraphicsPath path = new GraphicsPath(); path.AddRectangle(new RectangleF(0f, 0f, w, h)); Matrix mtrx = new Matrix(); mtrx.Rotate(angle); RectangleF rct = path.GetBounds(mtrx); Bitmap dst = new Bitmap((int)rct.Width, (int)rct.Height, pf); g = Graphics.FromImage(dst); g.Clear(bkColor); g.TranslateTransform(-rct.X, -rct.Y); g.RotateTransform(angle); g.InterpolationMode = InterpolationMode.HighQualityBilinear; g.DrawImageUnscaled(tmp, 0, 0); g.Dispose(); tmp.Dispose(); return dst; } / Bitmap robitmap; bool isrotate = false; private void button2_Click_1(object sender, EventArgs e) { var value = this.trackBar1.Value; var bmp = new Bitmap(PicSource.Image); if (!isrotate) { robitmap = bmp; isrotate = true; } var newBmp = KiRotate(robitmap, value, Color.Transparent); PicSource.Image = newBmp; }
裁剪图片
public static Bitmap PartDraw(Image src, Rectangle cutpart)//切割图片 { Bitmap rectbmp = new Bitmap(cutpart.Width, cutpart.Height, PixelFormat.Format24bppRgb); using (Graphics g = Graphics.FromImage(rectbmp)) { g.DrawImage(src, new Rectangle(0, 0, cutpart.Width, cutpart.Height), cutpart, GraphicsUnit.Pixel); g.Dispose(); } return rectbmp; }
注意!!
下面的button2_click绑定的是saveImg按钮
保存图片
private void button2_Click(object sender, EventArgs e) { saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif"; saveFileDialog1.Title = "Save an Image File"; saveFileDialog1.ShowDialog(); if (saveFileDialog1.FileName != "") { // 通过由OpenFile方法创建的FileStream保存图像。 System.IO.FileStream fs = (System.IO.FileStream)saveFileDialog1.OpenFile(); //根据以下内容将图像保存为适当的ImageFormat: //在对话框中选择的文件类型。 //请注意,FilterIndex属性是基于一的。 switch (saveFileDialog1.FilterIndex) { case 1: this.pictureBox1.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg); break; case 2: this.pictureBox1.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Bmp); break; case 3: this.pictureBox1.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Gif); break; } textBox1.AppendText("图片已保存至:"+ "E:\\小白人" + "\n"); textBox1.ScrollToCaret(); fs.Close(); } }
-
C#图片编辑处理 展示,皮肤类似网页的winform程序
2019-11-25 16:23:12C#图片编辑处理 展示,皮肤类似网页的winform程序 -
c# Base64编码和图片的互相转换代码
2021-01-01 08:25:23我们已经做了一个编辑器,这个编辑器可以以xml格式存储一些信息。在存储图片信息时我们碰到了一些问题。我们本来在xml信息中存储的是图片的路径,然而一旦客户把这个信息copy到其他电脑上而没有同时copy相关的图片... -
C# .NET MVC UEditor富文本编辑器
2019-04-30 18:39:35实现UEditor富文本编辑器与服务器的文件交互(图片,视频),该实例采用 C#后台开发语言完成! 请在vs2017中打开! -
C#利用GDI+给图片添加文字(文字自适应矩形区域)
2021-01-20 06:00:32这篇文章是 GDI+ 总结系列的第二篇,如果对 GDI+ 的基础使用不熟悉的朋友可以先看第一篇文章《C# 使用 GDI+ 画图》。 需求 需求是要做一个编辑文字的页面。用户在网页端写文字,文字区域是个矩形框,用户可以通过... -
Editor_WPF_C#_数学公式编辑器
2021-09-11 07:03:06WPF 数学公式编辑,公式图片导出,公式编辑导入编辑 -
C#获取缺陷图片并对其进行编辑操作
2019-08-27 15:30:34前台C#,后台.pc文件; 表面质量检查仪系统内有大量缺陷图片,利用一定逻辑查出典型照片,定期传入图片服务器中。 制造平台系统,按照卷号在查询某卷的缺陷时,会将和该卷相关图片显示在系统中,可以浏览、载入、... -
.NET下为百度文本编辑器UEditor增加图片删除功能示例
2021-01-02 01:22:04百度的这个编辑器,的确挺好,但也有些,让人不如意的,如每次更新,本来有功能给搞没了,旧版本的代码又不能直接拿过来组合用,对用户来很是不爽。今天下载了目前最新版1.2.5为版本看更新记录,主要是对表格做个... -
C# Winform按钮中图片实现左图右字的效果实例
2020-12-16 17:44:02设置流程 百度查阅了几个资料感觉没啥用,经过自己一番试验,总结了简单的方法,具体做法是 1.从工具栏拖出一个按钮 2.在vs外部 使用编辑进入到画图中使用...对了,在vs使用过程中去编辑图片会出现这种状况 关闭vs, -
C#练习创建图片编辑器
2013-04-14 18:44:46在本教程中,您将生成一个从文件加载图片并将其显示在窗口中的程序。 您将学习如何拖动控件(如窗体上的按钮和图片框)、设置控件属性,以及如何使用容器来平滑地调整窗体的大小。 您还将开始编写代码。 您将学习... -
c#文档编辑器
2018-12-24 11:22:30包含全部源码; 该文档编辑器,基本功能: 文件操作: 新建,打开,保存,退出; //支持rtf文件 编辑操作: 复制,剪切,粘贴,全选; 查找与替换: 设计查找(替换)窗口,支持查找(替换)操作 。...附加功能:插入图片 -
C#实现在listview中插入图片实例代码
2020-12-31 19:22:55C#实现在listview中插入图片实例代码 第一步:在窗体中拖入ListView控件和imageList控件; 第二步:设置imageList控件的Images属性,添加你想要的图片; 第三步:设置ListView控件的SmallImageList、LargeImageList... -
基于c#的图像编辑小程序
2021-04-01 12:05:36此程序包含对图像文件(bmp,jpg,gif)的各种编辑处理,包括灰化、浮雕、底片、添加暗角、降低亮度、马赛克、90逆时针旋转、水平翻转、垂直翻转。并且实现了文件的打开和保存。 -
C# GUI 截图工具 画图工具 图片编辑器 三合一 源码
2014-08-18 16:58:03C#操作的图片,其中包含截图功能,画图功能,以及简单的图片编辑功能。实现方法,通过GUI画图,以及对Bitmap图片处理实现。功能三合一哦 -
C# 图片编辑控件 dll
2011-02-10 15:58:56C# 图片编辑 控件功能 从本地文件载入图片,按比例放大缩小图片,缩放按钮, 使用控件的piceture字段对应显示的图片 dll文件 -
C#实现图片分割方法与代码
2020-12-26 06:20:41实现思路 .NET Framework GDI+ 为我们提供了一组丰富地类来编辑图形图像。有关.NET Framework GDI+的详细资料请查阅msdn相关文档。这里只简要叙述本程序要用的的几个类。 System.Drawing.Image.LoadFile ... -
C# 图片处理详解
2021-01-13 15:41:21本文主要介绍:获取图片的灰度/修改彩色图片为黑白图片/修改图片的对比度 Bitmap类:Bitmap对象封装了一个GDI+的位图,此位图由图形图像的像素,以及其属性组成。用于处理由像素数据定义的图像的对象,包括如下属性...本文主要介绍:获取图片的灰度/修改彩色图片为黑白图片/修改图片的对比度
Bitmap类:Bitmap对象封装了一个GDI+的位图,此位图由图形图像的像素,以及其属性组成。用于处理由像素数据定义的图像的对象,包括如下属性和方法
1.GetPixel方法和SetPixel方法:用于获取和设置图像的指定像素的颜色
2.PixelFormat属性:返回图像的像素格式
3.Palette属性:获取和设置图像所使用的颜色调色板
4.Height/Width属性:返回图像的高度和宽度
5.LockBits方法和UnlockBits是一个很好的方法这种方法可以使我们指定像素的范围来控制位图的任一部分,从而避免对位图的像素逐个进行处理,每调用一次LockBits都要调用UnlockBits进行解锁
BitmapData类:
1.Height/Width属性:被锁定位图的高度/宽度属性
2.PixelsFormat属性:数据实际像素格式
3.Scan0属性:被锁定数组的首字节地址,如果整个图像被锁定,则是图像的第一个字节地址
4.Stride属性:步幅,也称为扫描宽度
private string path=Application.StartupPath; private byte[] mappdata; private void button1_Click(object sender,EventArgs e) { Image img=null; Bitmap map=null; //读取对应图片 using(FileStream fs=new FileStream(path+"\\1.png",FileMode.Open)) { img=Image.FromStream(fs); } map=(Bitmap)img; //将图片转换为黑白图片 ChangeToGrayImg(map); //GetImageGrayData(map); //Bitmap map1=ChangeImageGrayOnPixel(map,10); //Bitmap map2=ChangeImageGrayOnMemory(map,100); //Bitmap map3=ChangeImageGrayOnPixel(map,10); }
//修改图片为黑白的三种算法 private void ChangeToGrayImg(Bitmap map) { int w=map.Width; int h=map.Height; Color pixel; for(int x=0;x<w;x++) { for(int y=0;y<h;y++) { pixel=map.GetPixel(x,y); int r=pixel.R; int g=pixel.G; int b=pixel.B; int result=0; int itype=2; switch(itype) { case 0://平均值法 result=((r+g+b)/3); break; case 1://最大值法 result=r>g?r:g; result=result>b?result:b; break; case 2://加权平均值法 result=((int)(0.7*r)+(int)(0.2*g)+(int)(0.1*b)); break; } map.SetPixel(x,y,Color.FromArgb(result,result,result)); } } //转换完成之后保存图片,这里没有重构,下面几个方法也能用 MemoryStream ms=new MemoryStream(); map.Save(ms,ImageFormat.Bmp); ms.Seek(0,SeekOrigin.Begin); byte[] buffer=new byte[ms.Length]; ms.Read(buffer,0,buffer.Length); ms.Dispose(); using(FileStream fs=new FileStream(path+"\\111.bmp",FileMode.CreateNew,FileAccess.Write)) { fs.Write(buffer,0,buffer.Length); } }
//根据图片获得图片的灰度数组 private void GetImageGrayData(Bitmap map) { BitmapData bmpData=map.LockBits(new Rectangle(0,0,map.Width,map.Height),ImageLockMode.ReadOnly,PixelFormat.Format8bppIndexed); //获取扫描线的宽度 int stride=bmpData.Stride; //显示宽度与扫描线宽度的间隙 int offset=stride.Width; //获取bmpData的内存起始位置 IntPtr iptr=bmpData.Scan0; //用stride宽度,表示这是内存区域的大小 int scanBytes=stride*map.Height; //为目标分配内存 mapdata=new byte[scanBytes]; //copy内存中的数据到目标数组中 System.Runtime.InteropServices.Marshal.Copy(iptr,mapdata,0,scanBytes); }
//基于像素和基于内存修改图片的对比度 //1.基于像素修改图片的对比度 public Bitmap ChangeImageGrayOnPixel(Bitmap bitmap,int degree/*对比度的加深比例*/) { Color curColor; int grayR,grayG,grayB; double Deg=(100.0+degree)/100.0; for(int i=0;i<bitmap.Width;i++) { for(int j=0;j<bitmap.Height;j++) { curColor=bitmap.GetPixel(i,j); grayR=Convert.ToInt16((((curColor.R/255.0-0.5)*Deg+0.5))*255); grayG=Convert.ToInt16((((curColor.G/255.0-0.5)*Deg+0.5))*255); grayB=Convert.ToInt16((((curColor.B/255.0-0.5)*Deg+0.5))*255); if(grayR<0) grayR=0; else if(grayR>255) grayR=255; if(grayG<0) grayG=0; else if(grayG>255) grayG=255; if(grayB<0) grayB=0; else if(grayB>255) grayB=255; bitmap.SetPixel(i,j,Color.FromArgb(grayR,grayG,grayB)); } } return bitmap; }
//2.基于内存修改图片的对比度 public unsafe Bitmap ChangeImageGrayOnMemory(Bitmap bitmap,int degree) { if(bitmap==null) { return null; } double Deg=(100+degree)/100.0; int width=bitmap.Width; int height=bitmap.Height; int length=height*3*width; byte[] RGB=new byte[length]; BitmapData data=bitmap.LockBits(new Rectangle(0,0,width,height),ImageLockMode.ReadWrite,PixelFormat.Format24bppRgb); IntPtr Scan0=data.Scan0; System.Runtime.InteropServices.Marshal.Copy(Scan0,RGB,0,length); double gray=0; for(int i=0;i<RGB.Length;i+=3) { for(int j=0;j<3;j++) { gray=(((RGB[i+j]/255.0-0.5)*Deg+0.5))*255.0; if(gray>255) { gray=255; } else if(gray<0) { gray=0; } RGB[i+j]=(byte)gray; } } System.Runtime.InteropServices.Marshal.Copy(RGB,0,Scan0,length); bitmap.UnlockBits(data); return bitmap; }