C#: Pixel width matching text rendered in a browser

I'm trying to estimate the widths in pixel if a text would be rendered in Chrome by using C# for a specific font (Arial 18px) in a tool that I'm creating. Comparing my results with this tool (uses the browser to render the width): http://searchwilderness.com/tools/pixel-length/ the string: "Lorem ipsum dolor sit amet, consectetur adipiscing elit." Is calculated to be 439 pixels wide.

C#:像素宽度匹配在浏览器中呈现的文本

我试图估计像素的宽度,如果在我创建的工具中使用C#为特定字体(Arial 18px)渲染文本。 将我的结果与此工具进行比较(使用浏览器呈现宽度):http://searchwilderness.com/tools/pixel-length/字符串: "Lorem ipsum dolor sit amet, consectetur adipiscing elit." 计算为439像素宽。 但是用C#中的这段代码,我得到了445px: var font = new Font("Arial", 18, FontStyle.Regular, GraphicsUnit.Pixel); var text = "L

stack allocated memory versus dynamically allocated memory

gcc 4.6.2 c89 I am just wondering what could cause more memory issues. For example, if you allocate a structure array on stack or if you was to allocate it dynamically on the heap. Normally, I follow a simple rule. If the structure is only to be used in that function, then I allocate on the stack. However, if I need to refer to it somewhere else by keeping it in memory I would allocate dyn

堆栈分配的内存与动态分配的内存

gcc 4.6.2 c89 我只是想知道什么可能导致更多的内存问题。 例如,如果您在堆栈中分配结构数组,或者如果您要在堆上动态分配它。 通常,我遵循一个简单的规则。 如果该结构仅用于该函数,那么我将在堆栈上分配。 但是,如果我需要通过将其保存在内存中来将其引用到其他位置,我将动态分配。 之所以我问这个问题是因为我的一些内存被损坏了,而且我被告知应该动态分配而不是在堆栈中为结构数组分配。 由于堆栈内存更容易

How to allocate aligned memory only using the standard library?

I just finished a test as part of a job interview, and one question stumped me - even using google for reference. I'd like to see what the stackoverflow crew can do with it: The “memset_16aligned” function requires a 16byte aligned pointer passed to it, or it will crash. a) How would you allocate 1024 bytes of memory, and align it to a 16 byte boundary? b) Free the memory after the mem

如何仅使用标准库分配对齐的内存?

作为面试的一部分,我刚刚完成了一项测试,其中一个问题难倒我 - 甚至使用谷歌作为参考。 我想看看stackoverflow的工作人员可以用它做些什么: “memset_16aligned”函数需要传递一个16byte对齐的指针,否则它会崩溃。 a)如何分配1024字节的内存,并将其与16字节的边界对齐? b)在memset_16aligned执行后释放内存。 { void *mem; void *ptr; // answer a) here memset_16aligned(ptr, 0, 1024); // a

Floating Point Div/Mul > 30 times slower than Add/Sub?

I recently read this post: Floating point vs integer calculations on modern hardware and was curious as to the performance of my own processor on this quasi-benchmark, so I put together two versions of the code, one in C# and one in C++ (Visual Studio 2010 Express) and compiled them both with optimizations to see what falls out. The output from my C# version is fairly reasonable: int add/sub: 3

浮点Div / Mul>比Add / Sub慢30倍?

我最近阅读了这篇文章:现代硬件上的浮点和整数计算,并且对我自己的处理器在这个准基准测试中的性能感到好奇,所以我将两个版本的代码放在一起,一个放在C#中,一个放在C ++中(Visual Studio 2010 Express),并通过优化对它们进行编译以查看哪些内容会掉落。 我的C#版本的输出是相当合理的: int add/sub: 350ms int div/mul: 3469ms float add/sub: 1007ms float div/mul: 67493ms double add/sub: 1914ms double div/mul

C# Func delegate in combination with lambda expression not correct

I have a class Employee, with some basic params, and a class employees that has a list of employee objects. I want to use the func delegate in the class employees to be able to use the following lambda expression in the main class. I think I have some syntax mistakes here... I'am getting the following error message: "cannot convert lambda expression to type 'Employee' because i

C#Func委托与lambda表达式组合不正确

我有一个Employee类,带有一些基本参数,以及一个拥有员工对象列表的班级员工。 我想在类员工中使用func委托,以便能够在主类中使用以下lambda表达式。 我想我在这里有一些语法错误...我得到以下错误消息:“不能转换lambda表达式键入'员工',因为它不是委托类型”。 有人知道我做错了什么? 非常感谢你们! static void Main(string[] args) { Employees lijst = new Employees();

Can a C# lambda expression have more than one statement?

Can a C# lambda expression include more than one statement? (Edit: As referenced in several of the answers below, this question originally asked about "lines" rather than "statements".) 当然: List<String> items = new List<string>(); var results = items.Where(i => { bool result; if (i == "THIS") re

一个C#lambda表达式可以有多个语句吗?

C#lambda表达式是否可以包含多个语句? (编辑:如下面几个答案中所引用的,这个问题最初是关于“行”而不是“语句”问的。) 当然: List<String> items = new List<string>(); var results = items.Where(i => { bool result; if (i == "THIS") result = true; else if (i == "THAT") result = true;

Func vs. Action vs. Predicate

This question already has an answer here: Delegates: Predicate Action Func 7 answers The difference between Func and Action is simply whether you want the delegate to return a value (use Func ) or not (use Action ). Func is probably most commonly used in LINQ - for example in projections: list.Select(x => x.SomeProperty) or filtering: list.Where(x => x.SomeValue == someOtherValue

功能与动作与谓词

这个问题在这里已经有了答案: 代表:Predicate Action Func 7个答案 Func和Action的区别在于你是否想让委托返回一个值(使用Func )或不使用(使用Action )。 Func可能在LINQ中最常用 - 例如在预测中: list.Select(x => x.SomeProperty) 或过滤: list.Where(x => x.SomeValue == someOtherValue) 或按键选择: list.Join(otherList, x => x.FirstKey, y => y.SecondKey, ...) Action更常用于List

converting a .net Func<T> to a .net Expression<Func<T>>

Going from a lambda to an Expression is easy using a method call... public void GimmeExpression(Expression<Func<T>> expression) { ((MemberExpression)expression.Body).Member.Name; // "DoStuff" } public void SomewhereElse() { GimmeExpression(() => thing.DoStuff()); } But I would like to turn the Func in to an expression, only in rare cases... public void ContainTheDanger(

将.net Func <T>转换为.net表达式<Func <T >>

使用方法调用从lambda转换为Expression很容易... public void GimmeExpression(Expression<Func<T>> expression) { ((MemberExpression)expression.Body).Member.Name; // "DoStuff" } public void SomewhereElse() { GimmeExpression(() => thing.DoStuff()); } 但我想将Func转换为表达式,仅在极少数情况下... public void ContainTheDanger(Func<T> dangerousCall) { try {

Retrieving Property name from lambda expression

Is there a better way to get the Property name when passed in via a lambda expression? Here is what i currently have. eg. GetSortingInfo<User>(u => u.UserId); It worked by casting it as a memberexpression only when the property was a string. because not all properties are strings i had to use object but then it would return a unaryexpression for those. public static RouteValueDict

从lambda表达式中检索属性名称

通过lambda表达式传入时是否有更好的方法来获取属性名称? 这是我目前拥有的。 例如。 GetSortingInfo<User>(u => u.UserId); 只有当属性是一个字符串时,它才会将它作为一个元素表达式进行处理。 因为不是所有的属性都是字符串,我不得不使用对象,但它会返回一个一元表达式。 public static RouteValueDictionary GetInfo<T>(this HtmlHelper html, Expression<Func<T, object>> action

Why aren't programs written in Assembly more often?

It seems to be a mainstream opinion that assembly programming takes longer and is more difficult to program in than a higher level language such as C. Therefore it seems to be recommend or assumed that it is better to write in a higher level language for these reasons and for the reason of better portability. Recently I've been writing in x86 assembly and it has dawned on me that perhaps th

为什么不是更频繁地使用Assembly编写的程序?

这似乎是一个主流的观点,程序集编程需要更长的时间,而且比C这样的高级语言编程更困难。因此,似乎推荐或假设由于这些原因而编写更高级的语言会更好一些并且为了更好的便携性。 最近我一直在使用x86汇编编写代码,它让我意识到可能这些原因不是真的,除了可移植性。 也许这更多的是熟悉和知道如何写好装配的问题。 我还注意到,在汇编中编程与在HLL中编程完全不同。 也许一个优秀且经验丰富的汇编程序员可以像编写C语言的