简体   繁体   English

我应该使用哪种类型来使用C#保存图像?

[英]What type should I use for saving images using C#?

I have this class called Hero that has the following attributes. 我有一个名为Hero的类,它具有以下属性。 String is for letters, int for numbers, etc. What can I use if I'm going to be saving an image there? 字符串用于字母,整数用于数字,等等。如果要在其中保存图像,该怎么办?

public class Hero
{
    [Hero class's attributes (non-image related]
    public byte[] Portrait { get; set; }
    public byte[] Screenshot { get; set; }
}

Portrait and Screenshot are going to be .png or .jpg files. 纵向和屏幕截图将是.png或.jpg文件。 Should I use a byte[] array for them? 我应该为他们使用byte []数组吗? I'm a bit confused. 我有点困惑。

LOL, nevermind. 大声笑,没关系。 I figured out my error. 我发现了我的错误。 I had to use System.WINDOWS.Media.Imaging. 我不得不使用System.WINDOWS.Media.Imaging。 Go figure. 去搞清楚。

I'm going to be using them for just showing them on a WPF Form. 我将使用它们仅在WPF表单上显示它们。

BitmapImage is good for XAML based apps, or another derived class of BitmapSource may be more appropriate for your needs, otherwise just a plain old Bitmap . BitmapImage非常适合基于XAML的应用程序,或者BitmapSource的另一个派生类可能更适合您的需求,否则只是一个普通的Bitmap

There is an actual Bitmap class . 有一个实际的Bitmap类 I would strongly suggest against using a Byte array for it as that it could become corrupted and you would invalidate the image. 我强烈建议您不要使用Byte数组,因为它可能会损坏,并且会使图像无效。 If you kept it in a Bitmap object, you can directly pass the object to the PictureBox control and render it on the screen. 如果将其保存在Bitmap对象中,则可以将对象直接传递给PictureBox控件 ,然后在屏幕上呈现它。

Additonally, unlike the BMP format PNG [assuming] and JPG do not store the pixel format in the same fashion or representation. 另外,与BMP格式不同,PNG [假定]和JPG不会以相同的方式或表示形式存储像素格式。 JPG does model fitting [within blocks] (where it gets some of its savings and loses information) and stores the representation of the model instead of direct color values. JPG进行模型拟合(在块内)(在其中进行部分节省并丢失信息),并存储模型的表示形式而不是直接的颜色值。

How are you going to be using them? 您将如何使用它们? The Image class might be more appropriate. Image类可能更合适。 Bitmap might be another good one. 位图可能是另一个不错的选择。

EDIT: 编辑:

Then you'll want to use a BitmapImage . 然后,您将要使用BitmapImage See also SO 94456 for some usage. 有关某些用法,另请参见SO 94456

It depends: 这取决于:

  1. If you need to manipulate that image (rotate, scale etc), then use Bitmap (or its base class - Image ). 如果需要操纵该图像(旋转,缩放等),请使用Bitmap (或其基类-Image )。
  2. If you store it just to pass the image to client (UI) then: 如果存储它只是为了将图像传递到客户端(UI),则:
    1. For web-application use array of bytes so it can be directly rendered to the response stream. 对于Web应用程序,使用字节数组,以便可以将其直接呈现到响应流。
    2. For the WPF application use the one that is easy to bind to (which can be BitmapImage , see related question ). 对于WPF应用程序,请使用易于绑定的应用程序(可以是BitmapImage ,请参阅相关问题 )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#数据结构:应该使用哪种类型的集合? - C# data structure: What type of collection should I use? 我应该使用什么类型来表示参数是C#中的文件名? - What type should I use to indicate an argument is a filename in C#? 我应该使用什么数据类型来表示 C# 中的钱? - What data type should I use to represent money in C#? 我应该在哪些类中使用C#的using语句? - What classes should I use C#'s using statement with? 我可以在类内创建C#方法吗?应该使用哪种类型的方法? - Can I create a C# method inside a class and what type of method should I use? 我应该使用哪种C#数据类型与非托管类型“ char *&sResult”进行交互 - What C# datatype should I use to interface with unmanaged type “char* &sResult” 我应该使用哪种类型的C#中的“类似数据库”的行为? - What type should i use for “database-like” behaviour in C#? 在C#中,应使用哪种数据类型存储具有标准偏差的数字列表? - In C#, what data type should I use to store a list of numbers with standard deviation? 编写需要嵌入加密数据库的C#桌面应用程序。我应该使用什么类型的数据库? - Writing a C# desktop application that needs to embed an encrypted database. What type of database should I use? 我应该使用哪种数据类型来读取年月和年周值? C# - What data type should I use for reading year-month and year-week values ? c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM