C#处理大图片
今天要用C#写个图片切割的软件(要求图片能够在picturebox中显示出来) 因为要处理的图片过大(150M左右)所以出现了一些问题 源代码如下
由于图片过大 所以首先想到先发图片压缩再显示出来 下面是一个图片压缩的类 网上找的
/// <summary>
/// 压缩图片类
/// </summary>
class ImgGDI
{
public ImgGDI()
{
//构造函数
}
/// <summary>
/// Bitmap转换byte[]数组
/// </summary>
/// <param name="bmp"></param>
/// <returns></returns>
public byte[] Bmptobyte(Bitmap bmp)
{
MemoryStream ms = new MemoryStream();
bmp.Save(ms, ImageFormat.Jpeg);
ms.Flush();
byte[] buffer = ms.GetBuffer();
ms.Close();
return buffer;
}
/// <summary>
/// byte[]数组转换Bitmap
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public Bitmap bytetobmp(byte[] buffer)
{
MemoryStream ms = new MemoryStream();
ms.Write(buffer, 0, buffer.Length);
Bitmap bmp = new Bitmap(ms);
ms.Close();
return bmp;
}
/// <summary>
/// 返回默认图片
/// </summary>
/// <returns></returns>
public Bitmap getInstance()
{
Bitmap bmp = DefaultPic();
return bmp;
}
/// <summary>
/// 选取本地图片
/// </summary>
/// <param name="IMG"></param>
/// <returns></returns>
public Bitmap LocalIMG(string IMG)
{
FileStream fs = new FileStream(IMG, FileMode.Open);
Bitmap bmp = new Bitmap(fs);
fs.Close();
return bmp;
}
/// <summary>
/// 返回流状态图片
/// </summary>
/// <param name="Img"></param>
/// <returns></returns>
public 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;
}
}
/// <summary>
/// 默认图片
/// </summary>
/// <returns></returns>
private Bitmap DefaultPic()
{
FileStream fs = new FileStream(Application.StartupPath + @"\Goodr.jpg", FileMode.Open);
Bitmap bmp = new Bitmap(fs);
fs.Close();
return bmp;
}
/// <summary>
/// GDI压缩图片
/// </summary>
/// <param name="bmp">传入参数Bitmap</param>
/// <returns></returns>
public byte[] ImageGdi(Bitmap bmp)
{
MemoryStream ms = new MemoryStream();
if (bmp != null)
{
Bitmap t = new Bitmap(bmp);//这里有问题,当图片较小的时候没有问题(7、8M左右)当图片大的时候提示构造函数的参数无效
t.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
}
byte[] buffer;
ms.Flush();
if (ms.Length > 95000)
{
//buffer = ms.GetBuffer();
double new_width = 0;
double new_height = 0;
Image m_src_image = Image.FromStream(ms);
if (m_src_image.Width >= m_src_image.Height)
{
new_width = 1024;
new_height = new_width * m_src_image.Height / (double)m_src_image.Width;
}
else if (m_src_image.Height >= m_src_image.Width)
{
new_height = 768;
new_width = new_height * m_src_image.Width / (double)m_src_image.Height;
}
Bitmap bbmp = new Bitmap((int)new_width, (int)new_height, m_src_image.PixelFormat);
Graphics m_graphics = Graphics.FromImage(bbmp);
m_graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
m_graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
m_graphics.DrawImage(m_src_image, 0, 0, bbmp.Width, bbmp.Height);
ms = new MemoryStream();
bbmp.Save(ms, ImageFormat.Jpeg);
buffer = ms.GetBuffer();
ms.Close();
return buffer;
}
else
{
buffer = ms.GetBuffer();
ms.Close();
return buffer;
}
}
}
以下是我的调用代码
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ImgGDI img = new ImgGDI();
Bitmap btp = img.LocalIMG("211.jpg");
byte[] bt = img.ImageGdi(btp);
Bitmap btpsmall = img.bytetobmp(bt);
pictureBox1.Image = (Image)btpsmall;
}
private void label1_Click(object sender, EventArgs e)
{
}
}
图片小的时候没有问题 图片大的时候(150M)时就出现了上面注释那里出现的错误 参数无效
请大家帮忙看看 帮忙改下算法 或者提出一种更好的算法 谢谢
由于图片过大 所以首先想到先发图片压缩再显示出来 下面是一个图片压缩的类 网上找的
/// <summary>
/// 压缩图片类
/// </summary>
class ImgGDI
{
public ImgGDI()
{
//构造函数
}
/// <summary>
/// Bitmap转换byte[]数组
/// </summary>
/// <param name="bmp"></param>
/// <returns></returns>
public byte[] Bmptobyte(Bitmap bmp)
{
MemoryStream ms = new MemoryStream();
bmp.Save(ms, ImageFormat.Jpeg);
ms.Flush();
byte[] buffer = ms.GetBuffer();
ms.Close();
return buffer;
}
/// <summary>
/// byte[]数组转换Bitmap
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public Bitmap bytetobmp(byte[] buffer)
{
MemoryStream ms = new MemoryStream();
ms.Write(buffer, 0, buffer.Length);
Bitmap bmp = new Bitmap(ms);
ms.Close();
return bmp;
}
/// <summary>
/// 返回默认图片
/// </summary>
/// <returns></returns>
public Bitmap getInstance()
{
Bitmap bmp = DefaultPic();
return bmp;
}
/// <summary>
/// 选取本地图片
/// </summary>
/// <param name="IMG"></param>
/// <returns></returns>
public Bitmap LocalIMG(string IMG)
{
FileStream fs = new FileStream(IMG, FileMode.Open);
Bitmap bmp = new Bitmap(fs);
fs.Close();
return bmp;
}
/// <summary>
/// 返回流状态图片
/// </summary>
/// <param name="Img"></param>
/// <returns></returns>
public 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;
}
}
/// <summary>
/// 默认图片
/// </summary>
/// <returns></returns>
private Bitmap DefaultPic()
{
FileStream fs = new FileStream(Application.StartupPath + @"\Goodr.jpg", FileMode.Open);
Bitmap bmp = new Bitmap(fs);
fs.Close();
return bmp;
}
/// <summary>
/// GDI压缩图片
/// </summary>
/// <param name="bmp">传入参数Bitmap</param>
/// <returns></returns>
public byte[] ImageGdi(Bitmap bmp)
{
MemoryStream ms = new MemoryStream();
if (bmp != null)
{
Bitmap t = new Bitmap(bmp);//这里有问题,当图片较小的时候没有问题(7、8M左右)当图片大的时候提示构造函数的参数无效
t.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
}
byte[] buffer;
ms.Flush();
if (ms.Length > 95000)
{
//buffer = ms.GetBuffer();
double new_width = 0;
double new_height = 0;
Image m_src_image = Image.FromStream(ms);
if (m_src_image.Width >= m_src_image.Height)
{
new_width = 1024;
new_height = new_width * m_src_image.Height / (double)m_src_image.Width;
}
else if (m_src_image.Height >= m_src_image.Width)
{
new_height = 768;
new_width = new_height * m_src_image.Width / (double)m_src_image.Height;
}
Bitmap bbmp = new Bitmap((int)new_width, (int)new_height, m_src_image.PixelFormat);
Graphics m_graphics = Graphics.FromImage(bbmp);
m_graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
m_graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
m_graphics.DrawImage(m_src_image, 0, 0, bbmp.Width, bbmp.Height);
ms = new MemoryStream();
bbmp.Save(ms, ImageFormat.Jpeg);
buffer = ms.GetBuffer();
ms.Close();
return buffer;
}
else
{
buffer = ms.GetBuffer();
ms.Close();
return buffer;
}
}
}
以下是我的调用代码
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ImgGDI img = new ImgGDI();
Bitmap btp = img.LocalIMG("211.jpg");
byte[] bt = img.ImageGdi(btp);
Bitmap btpsmall = img.bytetobmp(bt);
pictureBox1.Image = (Image)btpsmall;
}
private void label1_Click(object sender, EventArgs e)
{
}
}
图片小的时候没有问题 图片大的时候(150M)时就出现了上面注释那里出现的错误 参数无效
请大家帮忙看看 帮忙改下算法 或者提出一种更好的算法 谢谢
...全文
相关推荐
pic_handler.rar_C# 图片处理_c# 大图片压缩_图片压缩
CSharp写的图片处理工具,可批量将某文件夹下图片进行压缩,格式修改,文件名修改等操作
c#存储和读取图片
c#存储和读取图片,大图片处理,c#存储和读取图片,大图片处理
C#实现为一张大尺寸图片创建缩略图的方法
本文实例讲述了C#实现为一张大尺寸图片创建缩略图的方法。分享给大家供大家参考。具体实现方法如下: public static Bitmap CreateThumbnail(string lcFilename, int lnWidth, int lnHeight) { System.Drawing.Bitmap bmpOut = null; try { Bitmap loBMP = new Bitmap(lcFilename); ImageFormat loFormat = loBMP.RawFormat; decimal lnRatio; int
C#对超大图片处理
C# 操作200M以上的大图片 实现图片加雾效果,使用opencv技术 内附详细说明和所需库文件 适合研究GIS方向的开发人员借鉴...
c#后台处理图片压缩.txt
c#后台调整图片大小和质量压缩。有问题可以留言,我看到就会回复


