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

社区 C#

帖子详情

flexser 2007-03-22 10:26:47

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

...全文

2233 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#实现把图片转换成二进制以及把二进制转换成图片的方法示例

主要介绍了C#实现把图片转换成二进制以及把二进制转换成图片的方法,结合具体实例形式分析了基于C#的图片与二进制相互转换以及图片保存到数据库的相关操作技巧,需要的朋友可以参考下

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

C_图片转换成二进制流并且保存到sql_server数据库,或者存储。

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

VB.NET 使用Access数据库保存和读取图片文件;并连接到datagridview

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

C#做的显示服务器图片并保存以及对本地文件夹的操作都是写简单的东西我在做开发的时候写的练习

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

C# 对SqlServer中Image字段的读写的思路是将图片转换为二进制在向数据库写.

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

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Cheryll Lueilwitz

Last Updated:

Views: 5302

Rating: 4.3 / 5 (54 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Cheryll Lueilwitz

Birthday: 1997-12-23

Address: 4653 O'Kon Hill, Lake Juanstad, AR 65469

Phone: +494124489301

Job: Marketing Representative

Hobby: Reading, Ice skating, Foraging, BASE jumping, Hiking, Skateboarding, Kayaking

Introduction: My name is Cheryll Lueilwitz, I am a sparkling, clean, super, lucky, joyous, outstanding, lucky person who loves writing and wants to share my knowledge and understanding with you.