简体   繁体   English

如何从 leadtools 将位图转换为光栅图像

[英]How to convert a bitmap into a rasterimage from leadtools

I am trying to convert a screenshot from my screen as a bitmap into a rasterimage我正在尝试将屏幕截图作为位图转换为光栅图像

public static RasterImage TakeScreenShot()
        {
            // Capture a screenshot of the area of the screen containing the pixel
            using (Bitmap screenshot = new Bitmap(1920, 1080))
            {
                using (Graphics g = Graphics.FromImage(screenshot))
                {
                    g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(1920, 1080));

                    string path = System.IO.Path.Combine(Environment.GetFolderPath(
    Environment.SpecialFolder.MyDoc‌​uments), "bitmap.bmp");

                    screenshot.Save(path);
                    using (RasterCodecs codecs = new RasterCodecs())
                    {
                        RasterImage image = codecs.Load(path, 0, CodecsLoadByteOrder.BgrOrGray, 0, 0);
                        // The RasterImage object now contains the same image data as the Bitmap object
                        return image;
                    }
                }
            }

        }

I am currently getting an error at RasterImage image = codecs.Load(path, 0, CodecsLoadByteOrder.BgrOrGray, 0, 0);我目前在 RasterImage image = codecs.Load(path, 0, CodecsLoadByteOrder.BgrOrGray, 0, 0); 处遇到错误"Page not found" “找不到网页”

From documentation : 从文档

firstPage第一页
1-based index of the first page to load.要加载的第一个页面的基于 1 的索引。

lastPage最后一页
The 1-based index of the last page to load.要加载的最后一页的从 1 开始的索引。 Must be greater than or equal to firstPage.必须大于或等于 firstPage。 You can pass -1 to load from firstPage to the last page in the file.您可以传递 -1 以从文件中的 firstPage 加载到最后一页。

The page index starts with 1 but you pass 0 .页面索引以1开头,但您传递了0 So you could just try this:所以你可以试试这个:

RasterImage image = codecs.Load(path, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1);

As Stephan mentioned, if you want to save to a temp path and then reload it you need to pass the correct page number (1 in this instance) or use the overload method that loads the first page:正如 Stephan 提到的,如果您想保存到临时路径然后重新加载它,您需要传递正确的页码(在本例中为 1)或使用加载第一页的重载方法:

RasterImage image = codecs.Load(path);

Another option is using the RasterImageConverter to convert the Bitmap to RasterImage in memory instead of saving to a temp file like so:另一种选择是使用 RasterImageConverter 将位图转换为内存中的 RasterImage,而不是像这样保存到临时文件:

public static RasterImage TakeScreenShot()
{
    // Capture a screenshot of the area of the screen containing the pixel
    using (Bitmap screenshot = new Bitmap(1920, 1080))
    {
        using (Graphics g = Graphics.FromImage(screenshot))
        {
            g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(1920, 1080));

            RasterImage image = RasterImageConverter.ConvertFromImage(screenshot, ConvertFromImageOptions.None);
            return image;
        }
    }
}

This is in the Leadtools.Drawing assembly and here is the documentation link: ConvertFromImage这是在 Leadtools.Drawing 程序集中,这里是文档链接: ConvertFromImage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM