我想在Windows Phone 7应用程序中将BitmapImage转换为ByteArray.所以我尝试了这个,但它抛出了运行时异常"无效的指针异常".任何人都可以解释为什么我要做的事情抛出异常.您能为此提供替代解决方案吗?
public static byte[] ConvertToBytes(this BitmapImage bitmapImage)
{
byte[] data;
// Get an Image Stream
using (MemoryStream ms = new MemoryStream())
{
WriteableBitmap btmMap = new WriteableBitmap(bitmapImage);
// write an image into the stream
Extensions.SaveJpeg(btmMap, ms,
bitmapImage.PixelWidth, bitmapImage.PixelHeight, 0, 100);
// reset the stream pointer to the beginning
ms.Seek(0, 0);
//read the stream into a byte array
data = new byte[ms.Length];
ms.Read(data, 0, data.Length);
}
//data now holds the bytes of the image
return data;
}
Run Code Online (Sandbox Code Playgroud) 我们可以将类的Type属性限制为特定类型吗?
例如:
public interface IEntity { }
public class Entity : IEntity {}
public class NonEntity{}
class SampleControl {
public Type EntityType{get;set;}
}
Run Code Online (Sandbox Code Playgroud)
假设sampleControl是UI类(可能是Control,Form,..),其EntityType属性的值应该只接受typeof(Entity)的值,而不是typeof(NonEntity)我们如何限制用户赋予特定的在设计时键入(bcause - Sample是我们可以在设计时设置其属性的控件或表单),这在C#.net中是可行的
我们怎样才能使用C#3.0实现这一目标?
在我上面的类中,我需要Type属性,对此必须是IEntity之一.
我对序列化有疑问.
例:
interface IBase {}
[DataContract]
class Base : IBase
{
[DataContract]
public Derived Child{get;set;}
}
[DataContract]
[KnownType(typeof(Base))]
class Derived : Base
{
[DataMember]
public IBase Parrent {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试存储Base
类的实例IsolatedStorage
,它就不会被执行; 它挂了.有没有办法做到这一点?
压缩后如何获取压缩文件的扩展名System.IO.Compression.GZipStream
?
例如,如果原始文件已命名test.doc
并压缩test.gz
,我如何知道解压缩时要使用的文件扩展名?
我比较了两个具有相同值的不同类型的变量,
int i = 100;
short s = (short)100;
if (s == i)
{
return "Equals";
}
else
{
return "Not Equals";
}
float f = 100.5f;
double d = 100.5d;
if (d == f)
{
return "Equals";
}
else
{
return "Not Equals";
}
Run Code Online (Sandbox Code Playgroud)
第一个比较输出是"等于",第二个比较输出是"不等于"
我的问题是short和int的相同值是等于,如果它是等于那么为什么浮点数和双精度值的相同值不相等.
这不仅适用于float和double,如果我们比较十进制,它将显示编译器错误.
我需要为全景页面的标题设置图像,我们能够做到吗?实际上我使用TitleTemplate完成了这项工作,但它不起作用..你可以指导我如何将图像设置为全景页面标题.
守则是
<controls:Panorama Background="{Binding PanoramaBackground}">
<controls:Panorama.TitleTemplate>
<DataTemplate>
<StackPanel>
<Image x:Name="HeaderImage" Source="/Resources/header_logo.png" />
</StackPanel>
</DataTemplate>
</controls:Panorama.TitleTemplate>
</controls:Panorama>
Run Code Online (Sandbox Code Playgroud)
但这不起作用..
感谢致敬,
迪内希
我对CoVariance和ContraVariance有一些疑问..请参阅以下代码..
interface IGetElement<out T>
{
int Counter { get; }
T GetNext();
}
interface IAddElement<in T>
{
void Add(T t);
}
class Collection<T> : IAddElement<T>, IGetElement<T> where T : Fruit
{
List<T> tList = new List<T>();
private int _counter = 0;
public int Count { get { return tList.Count; } }
public void Add(T t)
{
tList.Add(t);
}
int IGetElement<T>.Counter
{
get { return _counter; }
}
public T GetNext()
{
return tList[_counter++];
}
public void Rset()
{
_counter = …
Run Code Online (Sandbox Code Playgroud) 我们可以为结构类型的变量赋值null吗?
struct MyStruct
{
}
MyStruct var = null;
Run Code Online (Sandbox Code Playgroud)
这是可能的C#.net?
如果不 ?那么C#如何允许Nullable <T>结构类型的变量可以指定为null?