C# drawing功能实现图片转换和编辑

using System;

 using System.Collections.Generic;

 using System.Text;

 using System.IO;

 using System.Drawing;

 using System.Drawing.Imaging;

 namespace VjsdnPublishProject

 {

 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;

 }

 ///

 /// 由图片文件转字节

 ///

 public static byte[] GetImageBytes(string imageFileName)

 {

 Image img = Image.FromFile(imageFileName);

 return GetImageBytes(img);

 }

 ///

 /// 图片转字节

 ///

 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; }

 }

 ///

 /// 字节转图片

 ///

 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; }

 }

 ///

 /// 将其它格式的图片转为JPG文件

 ///

 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);

 }

 ///

 /// 将其它格式的图片转为PNG文件

 ///

 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);

 }

 }

 }

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/22328375/viewspace-612573/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/22328375/viewspace-612573/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现GIS地图编辑功能需要掌握地图数据结构、地图数据处理、地图图层控制等技术。下面是一个简单的C#代码示例,实现添加点和线的功能: ```csharp using System.Drawing; using System.Windows.Forms; namespace GISEditor { public partial class MainForm : Form { private Point[] points = new Point[1000]; private int pointCount = 0; private Point[] lines = new Point[1000]; private int lineCount = 0; private bool isAddingPoint = false; private bool isAddingLine = false; public MainForm() { InitializeComponent(); } private void MainForm_MouseClick(object sender, MouseEventArgs e) { if (isAddingPoint) { points[pointCount++] = e.Location; isAddingPoint = false; Refresh(); } else if (isAddingLine) { lines[lineCount++] = e.Location; Refresh(); } } private void MainForm_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; // 绘制点 for(int i = 0; i < pointCount; i++) { g.FillEllipse(Brushes.Red, points[i].X, points[i].Y, 5, 5); } // 绘制线 if(lineCount > 1) { g.DrawLines(Pens.Blue, lines); } } private void addPointButton_Click(object sender, EventArgs e) { isAddingPoint = true; } private void addLineButton_Click(object sender, EventArgs e) { isAddingLine = true; } } } ``` 这个示例中,我们在一个窗口中添加了两个按钮,分别用于添加点和线。当我们点击“添加点”按钮时,鼠标会变成十字形,表示可以添加点,当我们点击鼠标时,会在窗口中添加一个红色的点。当我们点击“添加线”按钮时,鼠标同样会变成十字形,表示可以添加线,当我们依次点击鼠标时,会在窗口中添加一条蓝色的线。这个示例只是一个简单的演示,实际的GIS地图编辑功能需要更加复杂的数据结构和算法支持。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值