如何使用memorystream保存Image图片呢?-CSDN社区 (2024)

社区 C#

帖子详情

flexser 2007-03-22 10:26:47

请教大家一个问题,假如我已知了一个byteArrary,如何利用memoryStream把这个byteArrary保存为一个xxx.png图片呢?

...全文

2232 13

打赏 收藏

分享

转发到动态

举报

写回复

用AI写文章

13 条回复

切换为时间正序

请发表友善的回复…

发表回复

viena 2007-03-24

  • 打赏
  • 举报
回复

FileStream fs = new FileStream(@"c:\xxx.png");
fs.Write(byteArrary,0,fs.Length);
fs.Close();

viena 2007-03-24

  • 打赏
  • 举报
回复

FileStream fs = new FileStream(@"c:\xxx.png");
fs.Write(byteArrary);
fs.Close();

viena 2007-03-24

  • 打赏
  • 举报
回复

不是保存为图片吗?
用FileStream

dazhu2 2007-03-24

  • 打赏
  • 举报
回复

MemoryStream ms = new MemoryStream(byteArrary);
Image img = Image.FromStream(ms);
----------就这个方法

北京的雾霾天 2007-03-24

  • 打赏
  • 举报
回复

或者
MemoryStream ms = new MemoryStream(byteArrary);
Image img = Image.FromStream(ms);

北京的雾霾天 2007-03-24

  • 打赏
  • 举报
回复

ImageConverter imc = new ImageConverter();
Image _img = imc.ConvertFrom(null, System.Globalization.CultureInfo.CurrentCulture, byteArrary) as Image;
if (_img != null)
{
//处理_img;
return;
}

leixueqiyi 2007-03-23

  • 打赏
  • 举报
回复

public partial class validate : System.Web.UI.Page
{
private const int rndLength = 4;
protected void Page_Load(object sender, EventArgs e)
{
// 在此处放置用户代码以初始化页面
string vnum;
vnum = GetByRndNum();
Session["validatecode"] = vnum;

Response.ClearContent(); //需要输出图象信息 要修改HTTP头
//Response.BufferOutput = false;
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(GetByValidateCode(vnum).ToArray());
//Response.Close();

}

private MemoryStream GetByValidateCode(string VNum)//返回内存流
{


Bitmap Img = null;
Graphics g = null;
MemoryStream ms = null;
Random random = new Random();
int gheight = VNum.Length * 12;
Img = new Bitmap(gheight, 20);
g = Graphics.FromImage(Img);
Font f = new Font("Arial", 12, FontStyle.Bold);

g.Clear(GetByRandColor(180, 200));//设定背景色
Pen blackPen = new Pen(Color.AntiqueWhite, 1);
//Pen blackPen = new Pen(Color.Black ,1);
for (int i = 0; i < 32; i++)// 随机产生干扰线,使图象中的认证码不易被其它程序探测到
{


int x = random.Next(gheight);
int y = random.Next(20);
int xl = random.Next(6);
int yl = random.Next(12);
g.DrawLine(blackPen, x, y, x + xl, y + yl);
}
SolidBrush s = new SolidBrush(Color.SaddleBrown);
g.DrawString(VNum, f, s, 3, 3);
ms = new MemoryStream();
Img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);//Jpeg改Png即可
g.Dispose();
Img.Dispose();
return ms;
}
楼上的兄弟比较强
学学

Ki1381 2007-03-23

  • 打赏
  • 举报
回复

public static bool Bytes2File(byte[] b, string file)
{


bool rtn = false;

FileStream fs = null;

try
{


fs = new FileStream(file, FileMode.Create, FileAccess.Write);

fs.Write(b, 0, (int)b.Length);

rtn = true;
}
catch
{


rtn = false;
}
finally
{
fs.Close();
}

return rtn;
}

yuheshui123 2007-03-23

  • 打赏
  • 举报
回复

private void button2_Click_1(object sender, System.EventArgs e)
{
int iCanvasWidth = 2048;
int iCanvasHeight = 2048;
Bitmap bitmap = new Bitmap(iCanvasWidth, iCanvasHeight, PixelFormat.Format24bppRgb);
Graphics graph = Graphics.FromImage(bitmap);
graph.Clear(Color.White);
int iX = 40;
int iY = 40;
Font font = new Font("MS UI Gothic", 12);
SolidBrush brush = new SolidBrush(Color.Black);
SolidBrush brush2 = new SolidBrush(Color.Blue);
Pen pen = new Pen(Color.Black);
pen.EndCap = LineCap.ArrowAnchor;
pen.DashStyle = DashStyle.Solid;
#region
graph.DrawLine(pen, 200, 200, 200, 2000);
graph.DrawLine(pen, 200, 1000, 1000, 1000);
for (int f = 0; f < 20; f++)
{
graph.DrawLine(Pens.Black, 200 + iX * (f + 1), 1000, 200 + iX * (f + 1), 1000 - 5);

}
//Y軸
for (int f = 0; f < 40; f++)
{


graph.DrawLine(Pens.Black, 200, 200 + iY * (f + 1), 200 + 5, 200 + iY * (f + 1));
graph.DrawLine(Pens.Black, 200, 200 + iY * (f + 1), 200 + 5, 200 + iY * (f + 1));
}
#endregion
#region
double dblY1 = 1000;
double dblX1 = 200;
for (int i = 0; i < arrDatay.Count; i++)
{
double dblData = Convert.ToDouble(arrDatay[i]);
double dblY2 = 1000 + Math.Abs((dblData + 1.5) / (5 - 1.5) * 800);
double dblX2 = (Convert.ToDouble(arrxx[i]) - 1.5) / (5 - 1.5) * 800 + 200;
dblX1 = (Convert.ToDouble(arrxx[i]) - 1.5) / (5 - 1.5) * 800 + 203;
dblY1 = 1003 - (dblData - 1.5) / (4 - 1.5) * 800;
Pen blackPen = new Pen(Color.Blue, 1);
graph.DrawEllipse(blackPen, float.Parse(dblX2.ToString()), float.Parse(dblY2.ToString()), 4F, 4F);
}

for (int i = 0; i < arrData.Count; i++)
{


double dblData = Convert.ToDouble(arrData[i]);
double dblY2 = 1000 + Math.Abs((dblData + 1.5) / (5 - 1.5) * 800);
double dblX2 = (Convert.ToDouble(arrx[i]) - 1.5) / (5 - 1.5) * 800 + 200;
dblX1 = (Convert.ToDouble(arrx[i]) - 1.5) / (5 - 1.5) * 800 + 203;
dblY1 = 1003 - (dblData - 1.5) / (4 - 1.5) * 800;
Pen blackPen = new Pen(Color.Blue, 1);
graph.DrawEllipse(blackPen, float.Parse(dblX2.ToString()), float.Parse(dblY2.ToString()), 4F, 4F);
}
for (int i = 0; i < min_id - 20; i++)
{
arrxx2.Add((minx - xx19) / (min_id - 20) * i + xx19);
arrDatay2.Add(((minx - xx19) / (min_id - 20) * i + xx19) * m1 / m0 + m2 / m0);
}

Pen black3 = new Pen(Color.Black, 1);
for (int i = 0; i < arrDatay2.Count; i++)
{


double dblData = Convert.ToDouble(arrDatay2[i]);
double dblY2 = 10 + Math.Abs((dblData + 1.5) / (5 - 1.5) * 800);
double dblX2 = (Convert.ToDouble(arrxx2[i]) - 1.5) / (5 - 1.5) * 800 + 200;
graph.DrawEllipse(black3, float.Parse(dblX2.ToString()), float.Parse(dblY2.ToString()), 4F, 4F);
}

for (int i = 0; i < 20; i++)
{


int intX = 800 / 20 * i + 200;
//int intX = 2/200*i + 200;
int intY = 1000 - 800 / 20 * i;
int intY2 = 1000 + 800 / 20 * i;
Point ptx = new Point(intX, 1000 - 25);
Point pty = new Point(200, intY);
Point pty2 = new Point(200, intY2);
graph.DrawString(Convert.ToString(i * (5 - 1.5) / 20 + 1.5), font, brush2, pty);
graph.DrawString("-" + Convert.ToString((i) * (5 - 1.5) / 20 + 1.5), font, brush2, pty2);
graph.DrawString(((5 - 1.5) / 20 * i + 1.5).ToString(), font, brush2, ptx);
}
#endregion

string strFilePath = @"d:\study\jackey_gif2.gif";
bitmap.Save(strFilePath, ImageFormat.Gif);
arrx.Clear();
arrData.Clear();
arrxx.Clear();
arrDatay.Clear();
arrxx2.Clear();
arrDatay2.Clear();
font.Dispose();
brush.Dispose();
graph.Dispose();
bitmap.Dispose();
}

hertcloud 2007-03-22

  • 打赏
  • 举报
回复

//--下面是一个生成 验证码的程序代码 楼主看看应该就能明白 输出类型改成.png即可

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class validate : System.Web.UI.Page
{


private const int rndLength = 4;
protected void Page_Load(object sender, EventArgs e)
{
// 在此处放置用户代码以初始化页面
string vnum;
vnum = GetByRndNum();
Session["validatecode"] = vnum;

Response.ClearContent(); //需要输出图象信息 要修改HTTP头
//Response.BufferOutput = false;
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(GetByValidateCode(vnum).ToArray());
//Response.Close();

}

private MemoryStream GetByValidateCode(string VNum)//返回内存流
{


Bitmap Img = null;
Graphics g = null;
MemoryStream ms = null;
Random random = new Random();
int gheight = VNum.Length * 12;
Img = new Bitmap(gheight, 20);
g = Graphics.FromImage(Img);
Font f = new Font("Arial", 12, FontStyle.Bold);

g.Clear(GetByRandColor(180, 200));//设定背景色
Pen blackPen = new Pen(Color.AntiqueWhite, 1);
//Pen blackPen = new Pen(Color.Black ,1);
for (int i = 0; i < 32; i++)// 随机产生干扰线,使图象中的认证码不易被其它程序探测到
{


int x = random.Next(gheight);
int y = random.Next(20);
int xl = random.Next(6);
int yl = random.Next(12);
g.DrawLine(blackPen, x, y, x + xl, y + yl);
}
SolidBrush s = new SolidBrush(Color.SaddleBrown);
g.DrawString(VNum, f, s, 3, 3);
ms = new MemoryStream();
Img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);//Jpeg改Png即可
g.Dispose();
Img.Dispose();
return ms;
}
//-----------------给定范围获得随机颜色------------
Color GetByRandColor(int fc, int bc)
{
Random random = new Random();

if (fc > 255) fc = 255;
if (bc > 255) bc = 255;
//if(ac>255) ac=255;
int r = fc + random.Next(bc - fc);
int g = fc + random.Next(bc - fc);
int b = fc + random.Next(bc - bc);
Color rs = Color.FromArgb(r, g, b);
return rs;
}

//-----------------------取随机产生的认证码(N位数字)
public string GetByRndNum()
{


string Vchar = "0,1,2,3,4,5,6,7,8,9";//,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,w,x,y,z" ;
string[] VcArray = Vchar.Split(',');
string VNum = "";
//采用一个简单的算法以保证生成随机数的不同
Random rand = new Random();
int[] rndArr = new int[rndLength];
for (int i = 1; i < rndLength + 1; i++)
{
if(i>1)
{
do
{
rndArr[i-1] = rand.Next(10);//rand.Next(35) ;
bool goon = false;
for(int ix=0; ix<i-1; ix++)
{
if (rndArr[ix] == rndArr[i-1])
{
goon = true;
break;
}
}
if (!goon)
break;
} while (1 == 1);
}
else
rndArr[i-1] = rand.Next(10);
VNum += rndArr[i-1];
}
return VNum;
}
}

he_8134 2007-03-22

  • 打赏
  • 举报
回复

vs的帮助系统够强的了,一般不懂但又不难的东西我都自己找~~~

jinanjiang 2007-03-22

  • 打赏
  • 举报
回复

up

lovefootball 2007-03-22

  • 打赏
  • 举报
回复

http://www.guodong.net/blog/article.asp?id=238

如何使用memorystream保存Image图片呢?-CSDN社区 (15) C#实现把图片转换成二进制以及把二进制转换成图片的方法示例

在上面的代码中,我们首先使用MemoryStream将二进制数组转换成流,然后使用Image.FromStream将流转换成图片,并将其显示在PictureBox控件中。 三、图片保存到数据库中 在C#中,可以使用ADO.NET将图片保存到数据库...

如何使用memorystream保存Image图片呢?-CSDN社区 (16) C#二进制图片的存储或读取

读取数据库中的二进制流并将其转换回图片,我们需要先执行SQL查询获取数据,然后使用`MemoryStream`和`Image`类将二进制流还原为图片。以下是读取和显示图片的示例: ```csharp // 创建SqlCommand和数据库连接,与...

如何使用memorystream保存Image图片呢?-CSDN社区 (17) vb.net 使用Access数据库保存和读取图片文件

在VB.NET中,Access数据库常被用于小型项目的数据存储,包括保存和读取不同类型的数据,如文本、数字,甚至图片。本教程将详细介绍如何利用VB.NET与Access数据库交互,实现图片文件的保存与读取,并将结果展示在...

如何使用memorystream保存Image图片呢?-CSDN社区 (18) C#显示服务器图片并保存

至于保存图片到本地,可以使用`File.WriteAllBytes`方法将字节数组写入文件: ```csharp public void SaveImage(byte[] imageData, string filePath) { File.WriteAllBytes(filePath, imageData); } ``` 批量下载...

如何使用memorystream保存Image图片呢?-CSDN社区 (19) C# 对SqlServer中Image字段的读写(例子)

图片可以使用`System.Drawing.Image`类来处理,该类提供了将图片转换为字节数组和从字节数组还原图片的方法。 2. **读取数据库中的Image字段** - 使用`SqlConnection`连接到SQL Server数据库。 - 创建一个`...

如何使用memorystream保存Image图片呢?-CSDN社区 (2024)
Top Articles
Latest Posts
Recommended Articles
Article information

Author: Ray Christiansen

Last Updated:

Views: 5508

Rating: 4.9 / 5 (69 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Ray Christiansen

Birthday: 1998-05-04

Address: Apt. 814 34339 Sauer Islands, Hirtheville, GA 02446-8771

Phone: +337636892828

Job: Lead Hospitality Designer

Hobby: Urban exploration, Tai chi, Lockpicking, Fashion, Gunsmithing, Pottery, Geocaching

Introduction: My name is Ray Christiansen, I am a fair, good, cute, gentle, vast, glamorous, excited person who loves writing and wants to share my knowledge and understanding with you.