我正在尝试编写一个轻量级图像查看应用程序.但是,.NET存在系统内存限制.
当尝试加载大位图(9000 x 9000 px或更大,24位)时,我得到一个System.OutOfMemoryException.这是在具有2GB RAM(其中1.3GB用完)的Windows 2000 PC上.尝试加载文件也需要很长时间.
以下代码生成此错误:
Image image = new Bitmap(filename);
using (Graphics gfx = this.CreateGraphics())
{
gfx.DrawImage(image, new Point(0, 0));
}
Run Code Online (Sandbox Code Playgroud)
这段代码也是如此:
Stream stream = (Stream)File.OpenRead(filename);
Image image = Image.FromStream(stream, false, false);
using (Graphics gfx = this.CreateGraphics())
{
gfx.DrawImage(image, new Rectangle(0, 0, 100, 100), 4000, 4000, 100, 100, GraphicsUnit.Pixel);
}
Run Code Online (Sandbox Code Playgroud)
此外,它就足以做到这一点:
Bitmap bitmap = new Bitmap(filename);
IntPtr handle = bitmap.GetHbitmap();
Run Code Online (Sandbox Code Playgroud)
后一个代码旨在与GDI一起使用.在研究这个问题时,我发现这实际上是一个内存问题,.NET试图在单个内存块中分配两倍的内存.
http://bytes.com/groups/net-c/279493-drawing-large-bitmaps
我从其他应用程序(Internet Explorer,MS Paint等)了解到,可以打开大图像,而且很快.我的问题是,如何在.NET中使用大型位图?
无论如何流式传输它们,还是非内存加载它们?
当我遇到以下代码时,我正在重构一个简单的脚本文件解析器的旧代码:
StringReader reader = new StringReader(scriptTextToProcess);
StringBuilder scope = new StringBuilder();
string line = reader.ReadLine();
while (line != null)
{
switch (line[0])
{
case '$':
// Process the entire "line" as a variable,
// i.e. add it to a collection of KeyValuePair.
AddToVariables(line);
break;
case '!':
// Depending of what comes after the '!' character,
// process the entire "scope" and/or the command in "line".
if (line == "!execute")
ExecuteScope(scope);
else if (line.StartsWith("!custom_command"))
RunCustomCommand(line, scope);
else if …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Reactive Extensions (Rx) 来处理数据流。但是,每个元素的处理可能需要一些时间。为了中断处理,我使用了CancellationToken
,它有效地停止了订阅。
当请求取消时,如何优雅地完成当前工作并正确终止而不会丢失任何数据?
var cts = new CancellationTokenSource();
cts.Token.Register(() => Console.WriteLine("Token cancelled."));
var observable = Observable
.Interval(TimeSpan.FromMilliseconds(250));
observable
.Subscribe(
value =>
{
Console.WriteLine(value);
Thread.Sleep(500); // Simulate processing
if (cts.Token.IsCancellationRequested)
{
Console.WriteLine("Cancellation detected on {0}.", value);
Thread.Sleep(500); // Simulate some time consuming shutdown
Console.WriteLine("Cleaning up done for {0}.", value);
}
},
() => Console.WriteLine("Completed"),
cts.Token);
Console.ReadLine();
cts.Cancel();
Console.WriteLine("Job terminated.");
Run Code Online (Sandbox Code Playgroud)
0
1
2
Token cancelled.
Job terminated.
Cancellation detected on 2.
Cleaning up done for …
Run Code Online (Sandbox Code Playgroud) 我正在尝试学习如何使用RavenDB,为此我创建了一个基本的例子.似乎初始化商店和查询需要花费大量时间!
static void Main( string[] args )
{
const bool createNewEntities = true;
var sw = new Stopwatch();
using( var store = new EmbeddableDocumentStore {DataDirectory = "~\\Data"} )
{
sw.Start();
store.Initialize();
sw.Stop();
Console.WriteLine( "Initialized in {0} ms.", sw.ElapsedMilliseconds );
if (createNewEntities)
{
sw.Reset();
sw.Start();
using( var session = store.OpenSession() )
{
sw.Stop();
Console.WriteLine();
Console.WriteLine( "Opened session in {0} ms.", sw.ElapsedMilliseconds );
for( var i = 0; i < 10; i++ )
{
var entity = new EntityA( "Entity A " …
Run Code Online (Sandbox Code Playgroud) 我在 .NET 7 中创建了一个最小的 Web API,并且尝试将一些代码提取到单独的程序集中。此代码使用IResult,它是在 .NET 7 中引入的。
如何IResult
从常规班级图书馆进行参考?
根据MSDN,该类型应该Microsoft.AspNetCore.Http.Abstractions.dll
位于NuGet Microsoft.AspNetCore.App.Ref
v7.0.3中的程序集中,但那是一个平台组件,不能由程序集直接引用。
c# ×5
.net ×1
.net-core ×1
asp.net-core ×1
bitmap ×1
cancellation ×1
gdi ×1
minimal-apis ×1
parsing ×1
ravendb ×1