标签: system.drawing

矩形.X 与矩形.Left

我看到该类System.Drawing.Rectangle有两组属性:

  • X, Y, Width,Height
  • Left, Top, Right,Bottom

Width和之间的区别Right是显而易见的。Left但我不明白和属性背后的推理Top

X这些可以与和 一起互换使用Y,还是我遗漏了一些明显的东西?有什么时候应该使用哪个的约定吗?

.net c# system.drawing

4
推荐指数
1
解决办法
4133
查看次数

当磁盘上没有可用空间时,Image.Save上没有抛出异常

我正在服务器上保存图像,我正在覆盖磁盘没有可用空间的情况.为了模拟这一点,我创建了一个几乎没有空间的虚拟硬盘驱动器,我试图将图像保存在那里.

我希望在尝试保存图像时收到异常,但是:

using (var ms = new MemoryStream(myPictureStream))
using (Image image = Image.FromStream(ms))
{
    image.Save(fileName, ImageFormat.Jpeg);
}
Run Code Online (Sandbox Code Playgroud)

但事实并非如此,没有例外.而是保存一个空文件,所以我不知道错误.我想在尝试保存图像时收到一些错误,或者在抛出异常之前能够预测错误.

我已经考虑了一些可能的解决方案:

  • 我知道我可以检查驱动器上的可用空间DriveInfo,但是我没有驱动器号来检查生产,所以这个选项被丢弃了.
  • 保存图像后,我尝试检索它Image.FromFile然后OutOfMemoryException抛出,但我知道这可能发生在其他情况下,我不知道这是否是检查磁盘没有可用空间的合法方式.

所以......任何想法为什么Image.Save()在没有可用空间时不会抛出错误?关于如何处理这种情况的任何建议?

编辑

GetLastError没有帮助(它是代码0,成功),所以似乎处理这种情况的最佳方法是将图像保存为时间MemoryStream,然后将其写入文件,正如@Eldar和@nvoigt给出的答案所指出的那样.

这样做,IOException抛出一个,你可以检查HResult异常,看看它是否是一个不够空闲的磁盘空间(参见这个答案:https://stackoverflow.com/a/ 9294382/4067893).

c# system.drawing image out-of-memory

4
推荐指数
1
解决办法
394
查看次数

在 .Net Standard 中加载图像

我正在尝试加载一个 png 文件(其他格式是一个选项),以便在面向 .netstandard 1.4 的项目中的 OpenTk 中渲染为纹理,该项目不支持 System.Drawing 库。

我可以为此找到的每个 OpenTk 示例都取决于 System.Drawing.Bitmap 类。

这是我想在没有 System.Drawing 库的情况下创建的那种方法的示例,来自此Jitter Physics OpenGL Demo的纹理类

    void CreateFromBitmap(System.Drawing.Bitmap image)
    {
        image.RotateFlip(RotateFlipType.RotateNoneFlipY);

        GL.GenTextures(1, out name);
        GL.BindTexture(TextureTarget.Texture2D, name);

        // set pixel unpacking mode
        GL.PixelStore(PixelStoreParameter.UnpackSwapBytes, 0);
        GL.PixelStore(PixelStoreParameter.PackRowLength, 0);
        GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1);
        GL.PixelStore(PixelStoreParameter.UnpackSkipRows, 0);
        GL.PixelStore(PixelStoreParameter.UnpackSkipPixels, 0);

        BitmapData data = image.LockBits(new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
            ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

        // Requieres OpenGL >= 1.4
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.GenerateMipmap, 1); // 1 = True
        GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0,
                      OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0); …
Run Code Online (Sandbox Code Playgroud)

c# system.drawing image opentk .net-standard

4
推荐指数
1
解决办法
5383
查看次数

MigraDoc 和 .NET Core 2.0

我为 PDF 生成制作了一个类库。它是使用 PDFshart-MigraDoc(核心包*)实现的。类库本身使用 .NET Standard 作为其目标框架。

我可以在经典 C# 项目(如 WinForms)中使用类库,但是如果我尝试将它与 .NET Core 2.0 一起使用,我会收到以下错误:

无法加载文件或程序集“System.Drawing.Common,Version=0.0.0.0,Culture=neutral,PublicKeyToken=xxxx”。该系统找不到指定的文件。

我想这样做的原因可能是 MigraDoc 在其实现中使用了“System.Drawing”,(据我所知).NET Core 框架不支持它。

但是,这并没有真正解决我的问题,即我需要类库适用于所有 .NET 框架。根据这篇文章的建议,我尝试包含 NuGet 包CoreCompat.System.Drawing。但这一直没有效果,可能是因为 MigraDoc 还在尝试使用“原始”System.Drawing库。

有什么方法可以让我的类库适用于 .NET Core?还是我运气不好……?

注意:MigraDoc Core Package 与 .NET Core Framework 无关。名字的冲突只是巧合。

c# system.drawing migradoc .net-core

4
推荐指数
2
解决办法
8338
查看次数

在缩放模式图片框中平移矩形位置

我正在确定图像中的矩形区域并在图片框中将其显示给用户。
由于图像有时可能非常大,因此我使用的是将其SizeMode设置为Zoom.

我正在使用以下代码来转换矩形 (X, Y) 坐标:

public Point TranslateZoomMousePosition(Point coordinates)
{
    // test to make sure our image is not null
    if (pictureBox5.Image == null) return coordinates;
    // Make sure our control width and height are not 0 and our 
    // image width and height are not 0
    if (pictureBox5.Width == 0 || pictureBox5.Height == 0 || pictureBox5.Image.Width == 0 || pictureBox5.Image.Height == 0) return coordinates;
    // This is the one that gets a little tricky. …
Run Code Online (Sandbox Code Playgroud)

.net c# system.drawing picturebox winforms

4
推荐指数
2
解决办法
718
查看次数

处理静电刷

我正在写一个生物节律应用程序.为了测试它,我有一个带有Button和PictureBox的表单.当我点击按钮时,我做了

myPictureBox.Image = GetBiorhythm2();
Run Code Online (Sandbox Code Playgroud)

哪个第一次运行正常,但在第二次单击时会导致以下异常:

System.ArgumentException: Parameter is not valid.
   at System.Drawing.Graphics.CheckErrorStatus
   at System.Drawing.Graphics.FillEllipse
   at Larifari.Biorhythm.Biorhythm.GetBiorhythm2 in c:\delo\Horoskop\Biorhythm.cs:line 157
   at Larifari.test.Button1Click in c:\delo\Horoskop\test.Designer.cs:line 169
   at System.Windows.Forms.Control.OnClick
   at System.Windows.Forms.Button.OnClick
   at System.Windows.Forms.Button.OnMouseUp
   at System.Windows.Forms.Control.WmMouseUp
   at System.Windows.Forms.Control.WndProc
   at System.Windows.Forms.ButtonBase.WndProc
   at System.Windows.Forms.Button.WndProc
   at ControlNativeWindow.OnMessage
   at ControlNativeWindow.WndProc
   at System.Windows.Forms.NativeWindow.DebuggableCallback
   at ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop
   at ThreadContext.RunMessageLoopInner
   at ThreadContext.RunMessageLoop
   at System.Windows.Forms.Application.Run
   at Larifari.test.Main in c:\delo\Horoskop\test.cs:line 20
Run Code Online (Sandbox Code Playgroud)

导致错误的减少功能是:

public static Image GetBiorhythm2() {
        Bitmap bmp = new Bitmap(600, 300);
        Image img = bmp;
        Graphics g = Graphics.FromImage(img);

        Brush …
Run Code Online (Sandbox Code Playgroud)

c# asp.net system.drawing dispose brush

3
推荐指数
2
解决办法
3427
查看次数

System.Drawing:GDI +未正确初始化(内部GDI +错误)

ASP.NET Web站点随机引发此System.Drawing-error:

System.Runtime.InteropServices.ExternalException:System.Drawing.Bitmap..ctor(Stream stream)System.Drawing.ToolboxBitmapAttribute..cctor()

Exception information: 
    Exception type: TypeInitializationException 
    Exception message: The type initializer for 'System.Drawing.ToolboxBitmapAttribute' threw an exception. 

Stack trace:    at System.Reflection.CustomAttribute._CreateCaObject(Void* pModule, Void* pCtor, Byte** ppBlob, Byte* pEndBlob, Int32* pcNamedArgs)
   at System.Reflection.CustomAttribute.CreateCaObject(Module module, RuntimeMethodHandle ctor, IntPtr& blob, IntPtr blobEnd, Int32& namedArgs)
   at System.Reflection.CustomAttribute.GetCustomAttributes(Module decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit)
   at System.RuntimeType.GetCustomAttributes(Type attributeType, Boolean inherit)
   at System.ComponentModel.ReflectTypeDescriptionProvider.ReflectGetAttributes(Type type)
   at System.ComponentModel.ReflectTypeDescriptionProvider.ReflectedTypeData.GetAttributes()
   at System.ComponentModel.TypeDescriptor.TypeDescriptionNode.DefaultTypeDescriptor.System.ComponentModel.ICustomTypeDescriptor.GetAttributes()
   at System.ComponentModel.TypeDescriptor.GetAttributes(Type componentType)
   at System.Web.UI.ThemeableAttribute.IsTypeThemeable(Type …
Run Code Online (Sandbox Code Playgroud)

asp.net iis system.drawing gdi+

3
推荐指数
1
解决办法
7146
查看次数

计算缩放级别以使图像适合面板

如何计算缩放水平(图形比例)以使任何图像适合任何面板?

图像大小和图片大小可以是任何大小。

我需要的方法签名如下:

  public float CalculateZoomToFit(Image image, Panel targetPanel)
  {
     // I need to calculate the zoom level to make the picture fit into the panel
     return ???
  }
Run Code Online (Sandbox Code Playgroud)

提前致谢。

.net c# system.drawing 2d

3
推荐指数
1
解决办法
1943
查看次数

在HTML标记中使用System.Drawing.Image

我有一个可能很容易的问题.我有一个存储在数据库中的图像,然后我在我的应用程序中使用了一个C#方法来获取该图像并将其存储在一个如下所示的自定义类中:

public class MyImage
{
 public System.Drawing.Image myImageFromDB;
 public string imageName;
 public int imageNumberInCollection;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:我可以/如何在HTML图像标记中使用此类中的图像?我尝试了以下但它只返回一个带有红色X的框:

//myImageFromDBCollection is a list of MyImage objects
foreach(var ind in myImagesFromDBCollection)
{
  table += String.Format("<image src={0} />", ind.myImageFromDB);
}
Run Code Online (Sandbox Code Playgroud)

关于我做错了什么的任何想法?

html c# system.drawing

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

改变C#中的像素颜色

你好我正在研究读取整个图像的程序,并将绿线的颜色改为红线,例如我有这个图像 这个图片 我希望c#程序获得绿色像素并将其转换为红色我试过这段代码:`

public Bitmap ReadImgPixel(Bitmap img)
{
        Bitmap pic = new Bitmap(img,img.Width,img.Height);
        int a1 = img.Width;
        int a2 = img.Height;
        System.Drawing.Color[,] pixels = new System.Drawing.Color[a1,a2];

        for (int i = 0;i< img.Width ; i++)
        {
            for(int j=0; j < img.Height; j++)
            {

               System.Drawing.Color pxl = img.GetPixel(i, j);
                if (pxl != System.Drawing.Color.White)
                {
                    pic.SetPixel(i, j, System.Drawing.Color.Red);
                }

            }

        }
         return pic;
     }
Run Code Online (Sandbox Code Playgroud)

但结果是整个图像变成红色如何修复它!

.net c# system.drawing colors

3
推荐指数
1
解决办法
702
查看次数