我一直在编写一个PCX解码器,到目前为止,PCX图像本身解析得很好,但我无法弄清楚如何设置位图的调色板.
我创建了一个像这样的位图:
Bitmap bmp = new Bitmap(width,
height,
stride2,
System.Drawing.Imaging.PixelFormat.Format8bppIndexed,
pixels);
Run Code Online (Sandbox Code Playgroud)
但我似乎无法使用以下方法设置调色板:
for (int i = 0; i < 256; i += 3)
{
Color b = new Color();
b = Color.FromArgb(palette[i], palette[i + 1], palette[i + 2]);
bmp.Palette.Entries.SetValue(b, i);
}
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我已经读完了pcx文件调色板中的每个字节并将它们存储在palette []中.从那里,我用它来设置位图调色板中的条目.如何设置颜色?
我发现的人转换的负载BitmapSource为Bitmap约,但什么ImageSource来Bitmap?我正在制作一个成像程序,我需要从Image元素中显示的图像中提取位图.有谁知道如何做到这一点?
编辑1:
这是用于转换功能BitmapImage的Bitmap.请记住在编译器首选项中设置"unsafe"选项.
public static System.Drawing.Bitmap BitmapSourceToBitmap(BitmapSource srs)
{
System.Drawing.Bitmap btm = null;
int width = srs.PixelWidth;
int height = srs.PixelHeight;
int stride = width * ((srs.Format.BitsPerPixel + 7) / 8);
byte[] bits = new byte[height * stride];
srs.CopyPixels(bits, stride, 0);
unsafe
{
fixed (byte* pB = bits)
{
IntPtr ptr = new IntPtr(pB);
btm = new System.Drawing.Bitmap(width, height, stride, System.Drawing.Imaging.PixelFormat.Format1bppIndexed, ptr);
}
}
return btm; …Run Code Online (Sandbox Code Playgroud) 以下方法取自WinForms应用程序.它只是捕获屏幕,但我需要修改它以在WPF应用程序中工作.当我使用它时,它会返回一个黑色图像.尺寸是正确的.我没有任何打开的DirectX或视频,即使在我的桌面上它也无法正常工作.
public static Bitmap CaptureScreen()
{
// Set up a bitmap of the correct size
Bitmap CapturedImage = new Bitmap((int)SystemParameters.VirtualScreenWidth,
(int)SystemParameters.VirtualScreenHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
// Create a graphics object from it
System.Drawing.Size size = new System.Drawing.Size((int)SystemParameters.VirtualScreenWidth, (int)SystemParameters.VirtualScreenHeight);
using (Graphics g = Graphics.FromImage(CapturedImage))
{
// copy the entire screen to the bitmap
g.CopyFromScreen((int)SystemParameters.VirtualScreenWidth, (int)SystemParameters.VirtualScreenHeight, 0, 0,
size, CopyPixelOperation.SourceCopy);
}
return CapturedImage;
}
Run Code Online (Sandbox Code Playgroud)
谁能用我的方式向我显示错误?
我想知道当我的程序以命令行参数启动时(即传递文件名时)是否可以"关闭"我的主窗口自动加载.我遇到的问题是我的程序在单击与之关联的文件时加载,但是通过打开另一个主窗口并使用它来执行此操作.我遇到的问题是该程序后来仍然启动MainWindow,从而打开两个Windows,一个包含文件内容,另一个包含空.
如何防止空白窗口?在我看来,我要么阻止它打开主窗口,关闭主窗口或让程序将文件传递给主窗口.我的问题是,我不知道哪些是最好的或如何做到这一点.
这是代码:
protected override void OnStartup(StartupEventArgs e)
{
if (e.Args != null && e.Args.Count() > 0)
{
this.Properties["ArbitraryArgName"] = e.Args[0];
}
base.OnStartup(e);
if (Application.Current.Properties["ArbitraryArgName"] != null)
{
string fname = Application.Current.Properties["ArbitraryArgName"].ToString();
MainWindow mw = new MainWindow();
mw.Show();
mw.readVcard(fname);
Application.Current.Windows.
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
我的解决方案在底部.
我理解并阅读过使用WPF的FlowDocument在屏幕上创建XML样式文档,但是用户可以编辑的内容是可读的,还是只读的?如果是这样,这是怎么做到的?
我的问题主要集中在listitem控件的使用上,因为能够编辑在我的程序中使用的列表项的顺序会很好,而不是我必须创建自己的自定义控件.
问候.
一个能DescriptionAttribute在枚举包含TextBox"ES文本?我问,因为我有一个包含大量TextBoxes 的文件,我希望将它们的内容与我拥有的值相匹配.我怀疑我能做到这一点,但我完全不确定.
即
[DescriptionAttribute(textBox1.Text)]
a,
Run Code Online (Sandbox Code Playgroud) c# ×5
bitmap ×3
wpf ×3
image ×2
command-line ×1
enums ×1
flowdocument ×1
graphics ×1
mainwindow ×1
palette ×1
screenshot ×1