我正在尝试使用C#将32bpp截图图像转换为8bpp(或4bpp或1bpp)格式.我已经查看过类似主题的几个stackoverflow答案,大多数使用以下代码建议变体:
public static Bitmap Convert(Bitmap oldbmp)
{
Bitmap newbmp = new Bitmap(oldbmp.Width, oldbmp.Height, PixelFormat.Format8bppIndexed);
Graphics gr = Graphics.FromImage(newbmp);
gr.PageUnit = GraphicsUnit.Pixel;
gr.DrawImageUnscaled(oldbmp, 0, 0);
return newbmp;
}
Run Code Online (Sandbox Code Playgroud)
但是,当执行此操作时,我得到一个例外:A graphics object cannot be created from an image that has an indexed pixel format.据我所知,8,4和1bpp图像有颜色表映射而不是实际颜色像素本身(如32或16bpp图像),所以我假设我在某个地方缺少一些转换步骤,但我对C#相当新(即将到来)从C++背景),并希望能够做到这一点使用原生C#调用,而不是诉诸PInvoking BitBlt和GetDIBits等任何人能帮助我解决这个问题?谢谢.
编辑:我应该指出,我需要这个向后兼容.NET framework 2.0
我正在用C编写一个用于Mac(Leopard)的应用程序,它需要在接收电源通知时做一些工作,例如睡眠,唤醒,关机,重启.它launchd在登录时作为启动运行,然后开始监视通知.我用来做这个的代码如下:
/* ask for power notifications */
static void StartPowerNotification(void)
{
static io_connect_t rootPort;
IONotificationPortRef notificationPort;
io_object_t notifier;
rootPort = IORegisterForSystemPower(&rootPort, ¬ificationPort,
PowerCallback, ¬ifier);
if (!rootPort)
exit (1);
CFRunLoopAddSource (CFRunLoopGetCurrent(),
IONotificationPortGetRunLoopSource(notificationPort),
kCFRunLoopDefaultMode);
}
/* perform actions on receipt of power notifications */
void PowerCallback (void *rootPort, io_service_t y,
natural_t msgType, void *msgArgument)
{
switch (msgType)
{
case kIOMessageSystemWillSleep:
/* perform sleep actions */
break;
case kIOMessageSystemHasPoweredOn:
/* perform wakeup actions */
break;
case kIOMessageSystemWillRestart:
/* perform restart actions …Run Code Online (Sandbox Code Playgroud) 当我使用__FUNCTION__宏/变量打印出调试信息时,使用Microsoft C++编译器和gcc时输出的内容似乎有所不同.例如,使用以下简单代码:
class Foo
{
public:
void Bar(int a, int b, int c)
{
printf ("__FUNCTION__ = %s\n", __FUNCTION__);
}
};
int main (void)
{
Foo MyFoo;
MyFoo.Bar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到了使用Microsoft Visual C++编译器
__FUNCTION__ = Foo::Bar
Run Code Online (Sandbox Code Playgroud)
而在使用gcc编译时(在本例中是在Mac上),我得到了
__FUNCTION__ = Bar
Run Code Online (Sandbox Code Playgroud)
第二个例子并不理想,因为我经常有几个类,比方说Init()和Uninit()方法,并且在调试输出跟踪中,几乎不可能知道哪个类被调用,因为类名将丢失.现在,我知道你可以使用__PRETTY_FUNCTION__代替它__FUNCTION__来获得类似的东西
__PRETTY_FUNCTION__ = void Foo::Bar(int, int, int)
Run Code Online (Sandbox Code Playgroud)
这很好,但它对于我需要的东西有点过于冗长,并且对于具有大量参数的函数来说有点长.
所以我的问题是(最后),有没有办法让输出看起来像简单地Foo::Bar使用gcc,如上例所示?
我正在寻找Mac OS X上最简单或最合适的方式来简单地"发信号"或通知另一个进程.来自Windows背景,可以使用以下内容实现.
在流程A中:
// create named event
hCreatedEvent = CreateEvent(NULL, TRUE, FALSE, "MyUniqueNamedEvent");
// wait for it to be signalled
WaitForSingleObject(hCreatedEvent, INFINITE);
Run Code Online (Sandbox Code Playgroud)
然后在流程B中:
// open the existing named event
hOpenedEvent = OpenEvent(EVENT_ALL_ACCESS, FALSE, "MyUniqueNamedEvent");
// signal it
SetEvent(hOpenedEvent);
Run Code Online (Sandbox Code Playgroud)
因此,当SetEvent执行进程B中的调用时,进程A将突破WaitForSingleObject并执行一些工作.
我不需要实际发送任何数据,所以我排除了命名管道(FIFO)或套接字等等有点矫枉过正(我已经看过这个类似的问题,但是因为他们需要发送数据,我的问题略有不同).同样,我不会知道其他进程的PID(这就是为什么我需要某种共享对象)所以我不能使用任何需要它的东西.
到目前为止,我的候选名单是:
sem_open,sem_wait并sem_post创建/打开,等待和信号分别事件.看起来相当直接使用.notify(3)功能 - 使用起来相当简单,如果不是有点笨拙消费通知.NSDistributedNotificationCenter或者CFNotificationCenter功能 - 看起来是最像"Mac喜欢"的做事方式而且相当简单.但是,我的代码可能需要作为dylib运行,根据这个未解决的问题,这可能对我不起作用.那么,有没有人有任何建议/提示/恐怖故事使用上述任何一种,或者甚至更合适的替代品我没有想到实现我想要的?
如何以编程方式确定活动网络连接的网络连接链接速度 - 就像任务管理器在“网络”选项卡中显示的那样?我并不是真正追求可用带宽,只是当前连接的数字,例如 54Mbps、100Mbps 等。