标签: system.drawing

使用LockBits从内存创建的GDI +泛型错误保存位图

根据我在SO和网络上的研究,保存位图时的GDI +泛型错误显然是一个常见问题.给出以下简化的代码段:

byte[] bytes = new byte[2048 * 2048 * 2];

for (int i = 0; i < bytes.Length; i++)
{
    // set random or constant pixel data, whatever you want
}

Bitmap bmp = new Bitmap(2048, 2048, PixelFormat.Format16bppGrayScale);
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, 2048, 2048), ImageLockMode.ReadWrite, bmp.PixelFormat);
System.Runtime.InteropServices.Marshal.Copy(bytes, 0, bmpData.Scan0, 8388608);
bmp.UnlockBits(bmpData);
bmp.Save(@"name.bmp");
Run Code Online (Sandbox Code Playgroud)

这导致0x80004005一般错误.通常的原因据说是锁定组件,但我在这里看不到任何东西.我只是瞎了吗?我保存的路径存在,当然,只创建一个空的bmp文件(0B).

背景:我从使用C++/CLI包装器传输到.NET的相机驱动程序获取像素数据,因此上面的Bitmap对象由函数调用返回.但是,由于这个小例子已经失败,我猜这个适配器没有任何问题.

任何建议都非常感谢!

c# system.drawing gdi+

8
推荐指数
1
解决办法
3008
查看次数

创建缩略图,然后转换为字节数组

我有一段时间创建缩略图然后将它们转换为字节数组.我尝试了三种方法,并且我都遇到了3次错误.

第一个是使用Bitmap.GetThumbnailImage,我过去使用过,然后直接保存到Response.OutputStream

第二个是使用System.Drawing.Graphics和DrawImage().仍然没有去.

第三个是创建一个新的位图,传入旧的位图,并设置新的大小.同样的错误.

值不能为空.
参数名称:encoder

说明:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.

异常详细信息:System.ArgumentNullException:Value不能为null.
参数名称:encoder

源错误:
在执行当前Web请求期间生成了未处理的异常.可以使用下面的异常堆栈跟踪来识别有关异常的起源和位置的信息.

堆栈跟踪:
[ArgumentNullException:值不能为null.
参数名称:encoder]
System.Drawing.Image.Save(Stream stream,ImageCodecInfo encoder,EncoderParameters encoderParams)+615244

这是我的方法的代码.也许有人会看到我做错了什么.如果你不确定GetThumbSize,它只是一个方法,它接受图像大小和最大拇指大小,然后计算实际大小,以保持纵横比.

public static System.Drawing.Image.GetThumbnailImageAbort thumbnailCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);

    public static bool ThumbnailCallback()
    {
        return false;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="image"></param>
    /// <param name="size"></param>
    /// <remarks>
    /// This method will throw a AccessViolationException when the machine OS running the server code is windows 7.
    /// </remarks>
    /// <returns></returns>
    public static byte[] CreateThumbnail(byte[] imageData, Size size)
    {
        using (MemoryStream inStream …
Run Code Online (Sandbox Code Playgroud)

system.drawing bytearray thumbnails

7
推荐指数
1
解决办法
6416
查看次数

如何使用System.Drawing将旋转的字符串绘制为图像?

我正在为图像绘制字符串.图像的大小是动态的,或者换句话说,图像与显示字符串所需的一样大.为了实现这一点,我在使用Graphics.DrawString()渲染文本之前使用Graphics.MeasureString()测量大小.

这一切都很好,直到轮换发挥作用.到目前为止,我正在将字符串绘制到位图并旋转我得到的整个位图.

问题是我的调色板非常有限,没有混合颜色.所以我必须避免任何类型的抗锯齿,这只能通过旋转文本位图使用InterpolationMode.NearestNeighbor来实现.虽然这确保没有渲染出不需要的颜色,但结果确实非常难看(从用户的角度来看).

我的想法:应该可以通过使用Graphics.RotateTransform()旋转它来将文本绘制到位图并避免剪切,不是吗?

因为我必须首先定义要绘制的图像的大小,并且因为这个大小通过旋转而增加,所以我不知道如何完成这项工作.

任何帮助非常感谢!

c# system.drawing drawstring rotatetransform

7
推荐指数
1
解决办法
4637
查看次数

System.Drawing Out of Memory Exception

我的应用程序图形引擎抛出这些异常.它们都被空的捕获块消耗掉.在早期,我发现了一个没有被困的人(我回忆起与笔扩大有关).我用try和一个空的catch块包围它.似乎这些例外对所产生的图纸没有影响.我已经对此做了一些阅读而没有真正了解或深入了解它.

所以我的问题:

  1. 如果可以安全食用,为什么会抛出这些?和
  2. 忽视它们是否安全?我担心每个人都有一些隐藏的效果.我有内存泄漏,例如我从未找到过.

c# system.drawing

7
推荐指数
1
解决办法
1万
查看次数

Graphics.DrawImage意外调整图像大小

我有一些代码,它采用带有透明度的灰度图像的png,并尝试创建具有给定背景颜色的新图像(从数据库中查找)并将原始图像叠加在其上以创建图像高光和阴影所需的颜色.代码在ASP.NET上下文中运行,但我认为这不相关.

代码在我的本地计算机上运行良好,但是当它部署到我们的UAT服务器时,它会产生意想不到的结果.在UAT上,它创建了一个正确大小的图像,但阴影/高亮区域似乎在每个维度上缩小了20%.所以我看的主要图像最初为5x29,输出图像为5x29,但阴影区域为4x23.2(第24行略有不同,但主要是背景颜色,所以我假设它正在进行一些插值调整大小).

我失败的代码如下:

private byte[] GetImageData(CacheKey key)
{
    byte[] imageData;
    using (Image image = Image.FromFile(key.FilePath))
    using (Bitmap newImage = new Bitmap(image.Width, image.Height))
    {
        using (Graphics graphic = Graphics.FromImage(newImage))
        {
            using (SolidBrush brush = new SolidBrush(ColorTranslator.FromHtml(key.BackgroundColour)))
            {
                graphic.FillRectangle(brush, 0, 0, image.Width, image.Height);
            }
            graphic.DrawImage(image, 0, 0);

            /*
            The following lines see if there is a transparency mask to create final
             transparency. It does this using GetPixel and SetPixel and just modifying
             the alpha of newImage with the alpha of mask. I …
Run Code Online (Sandbox Code Playgroud)

.net c# system.drawing

7
推荐指数
1
解决办法
5222
查看次数

使用GetManifestResourceStream找不到嵌入式资源

我目前正在开发一个卡DLL,我需要将每张卡的图像文件作为嵌入式资源,当前项目如下所示:

卡片图像位于Resources文件夹中

注意:卡片图像(.png)位于Resources文件夹中.

我一直在尝试的代码,几乎是我能找到的唯一代码,是这样的:

Assembly _assembly;
Stream _imageStream;

private Image img;
public Image Img
{ 
get
    {
        return img;
    }
}

public Kaart() : this(5, "Spades") { }

public Kaart(int val, string s)
{
    this.KaartWaarde = val;
    this.Figuur = s;
    this._imageStream = imgS();
    this.img = new Image(_imageStream);
}

private Stream imgS()
{
    try
    {
        _assembly = Assembly.GetExecutingAssembly();
        Stream s = _assembly.GetManifestResourceStream(string.Format("{0}{1}.png", this.kaartWaarde, this.figuur.Substring(0, 1)));
        if (s != null)
            return s;
        else
            return null ;
    }
    catch
    {
        throw new Exception("Kaart kan …
Run Code Online (Sandbox Code Playgroud)

c# system.drawing stream embedded-resource

7
推荐指数
1
解决办法
3137
查看次数

.NET Core:在 Docker 中的 Linux 上使用 System.Drawing.Common - “无法打开显示”错误

我已将代码移植到使用 System.Drawing 的 .NET Core。目前,corefx 的 System.Drawing.Common 似乎支持Linux。但是我在 Linux 上运行我的代码有困难。

特别是我得到:

NotSupportedException “无法打开显示(需要 X-Server。检查您的 DISPLAY 环境变量)”

对于此代码:

Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
Run Code Online (Sandbox Code Playgroud)

以前我得到

DllNotFoundException “无法加载 DLL 'libX11':找不到指定的模块或其依赖项之一。\n(来自 HRESULT 的异常:0x8007007E)”

使用堆栈跟踪:

   at System.Drawing.LibX11Functions.XOpenDisplay(IntPtr display)
   at System.Drawing.Graphics.FromHwnd(IntPtr hwnd)
   at mycode
Run Code Online (Sandbox Code Playgroud)

但是我通过安装libx11-devpackage.json解决了这个问题。

这是我的 Dockerfile:

FROM microsoft/aspnetcore
ENV DOTNET_SKIP_FIRST_TIME_EXPERIENCE true

# install libgdiplus for System.Drawing
RUN apt-get update && \
    apt-get install -y --allow-unauthenticated libgdiplus libc6-dev

# install x11 for System.Drawing
RUN apt-get update && \
    apt-get install -y --allow-unauthenticated libx11-dev …
Run Code Online (Sandbox Code Playgroud)

linux x11 system.drawing .net-core

7
推荐指数
1
解决办法
5041
查看次数

EMF 图像大小和分辨率因机器的缩放设置而异 - 在 C# 中创建时

我正在 C# 中创建 EMF 图像,缩放模式为 125%(请参阅文章底部更新的链接以更改 Windows 机器中的缩放模式)。无论代码中使用的 DPI 设置如何,图像大小和分辨率都会根据机器的缩放设置而变化。

        //Set the height and width of the EMF image
        int imageWidth = 1280;
        int imageHeight = 720;

        //Adjust the witdh for the screen resoultion 
        using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
        {
            imageWidth = (int)(imageWidth / 96.0 * graphics.DpiX);
            imageHeight = (int)(imageHeight / 96.0 * graphics.DpiY);
        }

        Image image = null;

        //Stream to create a EMF image
        MemoryStream stream = new MemoryStream();

        //Create graphics with bitmap and render the graphics in stream …
Run Code Online (Sandbox Code Playgroud)

c# system.drawing image windows-10

7
推荐指数
0
解决办法
525
查看次数

使用 WIC 快速创建符合 EXIF 方向标签的缩小位图

我正在寻找最快的方法来创建按比例缩小的位图,以尊重 EXIF 方向标签

参考:https : //weblogs.asp.net/bleroy/the-fastest-way-to-resize-images-from-asp-net-and-it-s-more-supported-ish

目前我使用以下代码创建一个尊重 EXIF 方向标签的位图

  static Bitmap FixImageOrientation(Bitmap srce)
        {
            const int ExifOrientationId = 0x112;
            // Read orientation tag
            if (!srce.PropertyIdList.Contains(ExifOrientationId)) return srce;
            var prop = srce.GetPropertyItem(ExifOrientationId);
            var orient = BitConverter.ToInt16(prop.Value, 0);
            // Force value to 1
            prop.Value = BitConverter.GetBytes((short)1);
            srce.SetPropertyItem(prop);

            // Rotate/flip image according to <orient>
            switch (orient)
            {
                case 1:
                    srce.RotateFlip(RotateFlipType.RotateNoneFlipNone);
                    return srce;


                case 2:
                    srce.RotateFlip(RotateFlipType.RotateNoneFlipX);
                    return srce;

                case 3:
                    srce.RotateFlip(RotateFlipType.Rotate180FlipNone);
                    return srce;

                case 4:
                    srce.RotateFlip(RotateFlipType.Rotate180FlipX);
                    return srce;

                case 5:
                    srce.RotateFlip(RotateFlipType.Rotate90FlipX);
                    return srce; …
Run Code Online (Sandbox Code Playgroud)

.net c# wpf system.drawing wic

7
推荐指数
1
解决办法
396
查看次数

如何在 Asp.Net Core 3 中访问 System.Drawing 即 System.Drawing.Common

我正在将面向完整框架的 Asp.Net Core 2.2 网站转换为面向 .Net Core 3.1 的 Asp.Net Core 3.1 应用程序。我有点不清楚与 System.Drawing 相关的依赖关系以及如何实现它们。

我的项目使用 System.Drawing,在 Asp.Net Core 3.1 下编译时出现此错误,提示我添加了对以下内容的引用System.Drawing.Common.dll

错误 CS1069:在命名空间“System.Drawing”中找不到类型名称“Image”。此类型已转发到程序集“System.Drawing.Common, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51” 考虑添加对该程序集的引用。1-wwwGiftOasis3 C:\Users\Ron\source\repos\wwwGiftOasis3\wwwGiftOasis3\site\seller\profile\about\s-photos.cs

在 Visual Studio 中,我没有看到直接向裸机添加引用的方法,System.Drawing.Common.dll因此我假设我应该通过 NuGet 添加引用。

在 NuGet 中,我看到一个System.Drawing.Common看起来像我想要的包,但我不清楚我的项目是否满足依赖项:

在此处输入图片说明

我的项目显示了这些依赖项:

在此处输入图片说明

所以我有几个与这一切相关的问题:

1) 我的项目依赖于 Framework 的事实是否Microsoft.NETCore.App满足 NuGet 包的Microsoft.NETCore.Platforms依赖?

2) 我打算只在 Windows 上运行这个 Asp.Net Core 3.1 网站这一事实是否满足 NuGet 包的 Microsoft.Win32.SystemEvents依赖性?

system.drawing nuget asp.net-core asp.net-core-3.0

7
推荐指数
1
解决办法
6058
查看次数