我可以将System.Web.UI.WebControls.Image转换为字节 [英] can i convert System.Web.UI.WebControls.Image to bytes

查看:130
本文介绍了我可以将System.Web.UI.WebControls.Image转换为字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我可以将System.Web.UI.WebControls.Image转换为字节,以便将其保存到数据库.

实际上,我得到的图像是aspx页面(getimage.aspx)的响应,请参见下文.

Hi All,

Can i convert System.Web.UI.WebControls.Image to bytes so that i can save it to database.

Actually I am getting my image as a response of an aspx page (getimage.aspx), see below.

// send the image data down to the client
Response.Clear();
Response.AddHeader( "Content-Disposition", "attachment; filename=m-View_Image.jpg" );
Response.ContentType = "image/jpeg";
Response.TransmitFile( fileName );

// Writes the specified file directly to an HTTP response output stream
// without us having to buffer it in memory.
HttpContext.Current.ApplicationInstance.CompleteRequest();


我想读取该图像并将其保存在数据库中
我可以显示如下图像:


I want to read that image and save it in database
I can display the image like below:

string url = "http://..../getimage.aspx"

Image img = new Image();
img.ImageUrl = url;
img.DataBind();



但是如何将aspx响应中的图像转换为字节?

请帮帮我.

在此先感谢.



But how to convert the image coming as aspx response in to bytes ?

Please help me.

Thanks in Advance.

推荐答案

一切皆有可能.但是,我强烈建议不要序列化UI控件.

UI可以是软件项目的临时项目.如果序列化UI,则将无法以备份兼容的方式对其进行升级.以后很可能需要迁移到其他UI库或整个平台.您的数据库,繁忙性逻辑和其他特定于应用程序的代码将继续存在……避免创建UI与数据模型的硬耦合.

您需要的是松散耦合,请参见 http://en.wikipedia.org/wiki/Loose_coupling [^ ].

无需序列化UI,而是创建UI的逻辑数据模型.它将与您的功能而不是与UI元素紧密绑定.该模型应由纯数据类/结构组成.数据模型和UI之间的关系将简化为两个操作:数据到UI(填充)和UI到数据(获取).是的,这是额外的工作,但在不久的将来会得到很好的回报.

这样的模型的序列化将不是问题.我建议您不要手动执行此操作.我最好的建议是使用数据合同,请参见 http://msdn.microsoft. com/en-us/library/ms733127.aspx [ ^ ].

这种序列化方法是最不介入的.您无需修改​​任何类型就可以使用System.Runtime.Serialization.DataContractSerializerSystem.Runtime.Serialization.Json.DataContractJsonSerializer对其进行序列化;相反,您只需将属性添加到类型和成员中.

当您对UI序列化的思考显示出对应用程序体系结构的困惑时,我建议您熟悉我在过去的解决方案中列出的建筑模式:
如何控制用户界面表单的控件通过函数(方法) [ ^ ].

以下是一般的建筑模式说明: http://en.wikipedia.org/wiki/Architectural_pattern_(computer_science) [ ^ ].

您不必学习它们,但实际上您需要对如何构建健壮的应用程序体系结构有个好主意.

—SA
Everything is possible. However, I strongly recommend not to serialize UI controls.

UI can be a temporary item of a software project. If you serialize UI, you won''t be able to upgrade it in a backup-compatible manner. It''s very likely that later on you will need to migrate to a different UI library or a whole platform. You database, busyness logic and other application specific code will survive… in you refrain from creation of hard coupling of UI with data model.

What you need is loose coupling, see http://en.wikipedia.org/wiki/Loose_coupling[^].

Instead of serializing the UI, create a logical data model of you UI. It will be tightly bound to your functionality and not to UI elements. The model should be composed of pure data classes/structures. The relationship between data model and UI will be reduced to two operations: data to UI (population) and UI to data (acquisition). Yes, this is extra work, but it will pay very well of in near future.

Serialization of such model won''t be a problem. I suggest you don''t do it manually. My best advice is using Data Contract, see http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

This method of serialization is the most non-intrusive. You don''t have to modify any of your types to make them serializable with System.Runtime.Serialization.DataContractSerializer or System.Runtime.Serialization.Json.DataContractJsonSerializer; instead, you only add attribute to your types and members.

As your thinking about UI serialization shows pretty good confusion about application architecture, I would advice to familiarize yourself with the architectural patterns I listed in my past solution:
how to control Controlls of a user interface form through Functions (methods)[^].

Here is the explanation of architectural patterns in general: http://en.wikipedia.org/wiki/Architectural_pattern_(computer_science)[^].

You don''t have to learn them, but you really need to get a good idea on how robust application architecture should be build.

—SA


这篇关于我可以将System.Web.UI.WebControls.Image转换为字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆