Skip to main content
Became Hot Network Question
deleted 76 characters in body
Source Link
Short Int
  • 762
  • 2
  • 14
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

public record class IMG(byte[] Data)
{
    const ImageLockMode Read = ImageLockMode.ReadOnly;
    
    const ImageLockMode Write = ImageLockMode.WriteOnly;
    
    const PixelFormat Format = PixelFormat.Format32bppArgb;
     
    // Start Size Approximation

    private readonly int Size = Data.Length >> 2;
    
    public int Width => GetWidthGetSize(Math.Sqrt(Size));

    public int Height => GetHeightGetSize((double)Size / Width);
 
    private static int GetHeight(double x) => (int)Math.Ceiling(x);

   
   private static int GetWidthGetSize(intdouble x) => (int)Math.Ceiling(Math.Sqrt(x));
    
    // End Size Approximation
    
    public Bitmap Build()
    {
        var rect = new Rectangle(0, 0, Width, Height);                                
        var result = new Bitmap(Width, Height, Format);                
        var bitData = result.LockBits(rect, Write, Format);
        Marshal.Copy(Data, 0, bitData.Scan0, Data.Length);
        result.UnlockBits(bitData);
        return result;
    }

    [DebuggerStepThrough, DebuggerHidden]
    public static IMG? Load(Bitmap bitmap)
    {
        ArgumentNullException.ThrowIfNull(bitmap);
        if (bitmap.PixelFormat != Format)
            throw new NotSupportedException(nameof(bitmap));
        var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
        var bitData = bitmap.LockBits(rect, Read, Format);
        var size = bitData.Height * bitData.Stride;
        var result = new byte[size];
        Marshal.Copy(bitData.Scan0, result, 0, size);
        bitmap.UnlockBits(bitData);
        return new(result);                
    }
}
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

public record class IMG(byte[] Data)
{
    const ImageLockMode Read = ImageLockMode.ReadOnly;
    
    const ImageLockMode Write = ImageLockMode.WriteOnly;
    
    const PixelFormat Format = PixelFormat.Format32bppArgb;
     
    // Start Size Approximation

    private readonly int Size = Data.Length >> 2;
    
    public int Width => GetWidth(Size);

    public int Height => GetHeight((double)Size / Width);
 
    private static int GetHeight(double x) => (int)Math.Ceiling(x);

    private static int GetWidth(int x) => (int)Math.Ceiling(Math.Sqrt(x));
    
    // End Size Approximation
    
    public Bitmap Build()
    {
        var rect = new Rectangle(0, 0, Width, Height);                                
        var result = new Bitmap(Width, Height, Format);                
        var bitData = result.LockBits(rect, Write, Format);
        Marshal.Copy(Data, 0, bitData.Scan0, Data.Length);
        result.UnlockBits(bitData);
        return result;
    }

    [DebuggerStepThrough, DebuggerHidden]
    public static IMG? Load(Bitmap bitmap)
    {
        ArgumentNullException.ThrowIfNull(bitmap);
        if (bitmap.PixelFormat != Format)
            throw new NotSupportedException(nameof(bitmap));
        var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
        var bitData = bitmap.LockBits(rect, Read, Format);
        var size = bitData.Height * bitData.Stride;
        var result = new byte[size];
        Marshal.Copy(bitData.Scan0, result, 0, size);
        bitmap.UnlockBits(bitData);
        return new(result);                
    }
}
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

public record class IMG(byte[] Data)
{
    const ImageLockMode Read = ImageLockMode.ReadOnly;
    
    const ImageLockMode Write = ImageLockMode.WriteOnly;
    
    const PixelFormat Format = PixelFormat.Format32bppArgb;
     
    // Start Size Approximation

    private readonly int Size = Data.Length >> 2;

    public int Width => GetSize(Math.Sqrt(Size));

    public int Height => GetSize((double)Size / Width);
             
    static int GetSize(double x) => (int)Math.Ceiling(x);

    // End Size Approximation
    
    public Bitmap Build()
    {
        var rect = new Rectangle(0, 0, Width, Height);                                
        var result = new Bitmap(Width, Height, Format);                
        var bitData = result.LockBits(rect, Write, Format);
        Marshal.Copy(Data, 0, bitData.Scan0, Data.Length);
        result.UnlockBits(bitData);
        return result;
    }

    [DebuggerStepThrough, DebuggerHidden]
    public static IMG? Load(Bitmap bitmap)
    {
        ArgumentNullException.ThrowIfNull(bitmap);
        if (bitmap.PixelFormat != Format)
            throw new NotSupportedException(nameof(bitmap));
        var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
        var bitData = bitmap.LockBits(rect, Read, Format);
        var size = bitData.Height * bitData.Stride;
        var result = new byte[size];
        Marshal.Copy(bitData.Scan0, result, 0, size);
        bitmap.UnlockBits(bitData);
        return new(result);                
    }
}
deleted 6 characters in body
Source Link
Short Int
  • 762
  • 2
  • 14
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

public record class IMG(byte[] Data)
{
    const ImageLockMode Read = ImageLockMode.ReadOnly;
    
    const ImageLockMode Write = ImageLockMode.WriteOnly;
    
    const PixelFormat Format = PixelFormat.Format32bppArgb;
     
    // Start Size Approximation

    private readonly int Size = Data.Length >> 2;
    
    public int Width => GetValueGetWidth(Size);

    public int Height => GetValueGetHeight((double)Size / Width);
    
    private static int GetValueGetHeight(double x) => (int)Math.Ceiling(x);
    
    private static int GetValueGetWidth(int x) => (int)Math.Ceiling(Math.Sqrt(x));
    
    // End Size Approximation
    
    public Bitmap Build()
    {
        var rect = new Rectangle(0, 0, Width, Height);                                
        var result = new Bitmap(Width, Height, Format);                
        var bitData = result.LockBits(rect, Write, Format);
        Marshal.Copy(Data, 0, bitData.Scan0, Data.Length);
        result.UnlockBits(bitData);
        return result;
    }

    [DebuggerStepThrough, DebuggerHidden]
    public static IMG? Load(Bitmap bitmap)
    {
        ArgumentNullException.ThrowIfNull(bitmap);
        if (bitmap.PixelFormat != Format)
            throw new NotSupportedException(nameof(bitmap));
        var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
        var bitData = bitmap.LockBits(rect, Read, Format);
        var size = bitData.Height * bitData.Stride;
        var result = new byte[size];
        Marshal.Copy(bitData.Scan0, result, 0, size);
        bitmap.UnlockBits(bitData);
        return new(result);                
    }
}
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

public record class IMG(byte[] Data)
{
    const ImageLockMode Read = ImageLockMode.ReadOnly;
    
    const ImageLockMode Write = ImageLockMode.WriteOnly;
    
    const PixelFormat Format = PixelFormat.Format32bppArgb;
     
    // Start Size Approximation

    private readonly int Size = Data.Length >> 2;
    
    public int Width => GetValue(Size);

    public int Height => GetValue((double)Size / Width);
    
    private static int GetValue(double x) => (int)Math.Ceiling(x);
    
    private static int GetValue(int x) => (int)Math.Ceiling(Math.Sqrt(x));
    
    // End Size Approximation
    
    public Bitmap Build()
    {
        var rect = new Rectangle(0, 0, Width, Height);                                
        var result = new Bitmap(Width, Height, Format);                
        var bitData = result.LockBits(rect, Write, Format);
        Marshal.Copy(Data, 0, bitData.Scan0, Data.Length);
        result.UnlockBits(bitData);
        return result;
    }

    [DebuggerStepThrough, DebuggerHidden]
    public static IMG? Load(Bitmap bitmap)
    {
        ArgumentNullException.ThrowIfNull(bitmap);
        if (bitmap.PixelFormat != Format)
            throw new NotSupportedException(nameof(bitmap));
        var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
        var bitData = bitmap.LockBits(rect, Read, Format);
        var size = bitData.Height * bitData.Stride;
        var result = new byte[size];
        Marshal.Copy(bitData.Scan0, result, 0, size);
        bitmap.UnlockBits(bitData);
        return new(result);                
    }
}
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

public record class IMG(byte[] Data)
{
    const ImageLockMode Read = ImageLockMode.ReadOnly;
    
    const ImageLockMode Write = ImageLockMode.WriteOnly;
    
    const PixelFormat Format = PixelFormat.Format32bppArgb;
     
    // Start Size Approximation

    private readonly int Size = Data.Length >> 2;
    
    public int Width => GetWidth(Size);

    public int Height => GetHeight((double)Size / Width);

    private static int GetHeight(double x) => (int)Math.Ceiling(x);

    private static int GetWidth(int x) => (int)Math.Ceiling(Math.Sqrt(x));
    
    // End Size Approximation
    
    public Bitmap Build()
    {
        var rect = new Rectangle(0, 0, Width, Height);                                
        var result = new Bitmap(Width, Height, Format);                
        var bitData = result.LockBits(rect, Write, Format);
        Marshal.Copy(Data, 0, bitData.Scan0, Data.Length);
        result.UnlockBits(bitData);
        return result;
    }

    [DebuggerStepThrough, DebuggerHidden]
    public static IMG? Load(Bitmap bitmap)
    {
        ArgumentNullException.ThrowIfNull(bitmap);
        if (bitmap.PixelFormat != Format)
            throw new NotSupportedException(nameof(bitmap));
        var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
        var bitData = bitmap.LockBits(rect, Read, Format);
        var size = bitData.Height * bitData.Stride;
        var result = new byte[size];
        Marshal.Copy(bitData.Scan0, result, 0, size);
        bitmap.UnlockBits(bitData);
        return new(result);                
    }
}
deleted 471 characters in body
Source Link
Short Int
  • 762
  • 2
  • 14
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

public record class IMG(byte[] Data)
{
    const ImageLockMode Read = ImageLockMode.ReadOnly;
    
    const ImageLockMode Write = ImageLockMode.WriteOnly;
    
    const PixelFormat Format = PixelFormat.Format32bppArgb;
     
    // Start Size Approximation

    private readonly int Size = Data.Length >> 2;
    
    public int Width => GetValue(Size);

    public int Height => GetValue((double)Size / Width);
    
    private static int GetValue(double x) => (int)Math.Ceiling(x);
    
    private static int GetValue(int x) => (int)Math.Ceiling(Math.Sqrt(x));
    
    // End Size Approximation
    
    public Bitmap Build()
    {
        var rect = new Rectangle(0, 0, Width, Height);                                
        var result = new Bitmap(Width, Height, Format);                
        var bitData = result.LockBits(rect, Write, Format);
        Marshal.Copy(Data, 0, bitData.Scan0, Data.Length);
        result.UnlockBits(bitData);
        return result;
    }

    [DebuggerStepThrough, DebuggerHidden]
    public static IMG? Load(Bitmap bitmap)
    {
        ArgumentNullException.ThrowIfNull(bitmap);
        try
        {
            var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            Bitmap temp = bitmap;                    
            if (bitmap.PixelFormat != Format)
            {
                temp =throw new(rect.Width, rect.Height, Format);
                using Graphics graphics = Graphics.FromImageNotSupportedException(tempnameof(bitmap));
          var rect = new Rectangle(0, 0, graphics.DrawImageUnscaledAndClipped(bitmap.Width, rectbitmap.Height);
            }                    
            var bitData = tempbitmap.LockBits(rect, Read, Format);
            var size = bitData.Height * bitData.Stride;
            var result = new byte[size];
            Marshal.Copy(bitData.Scan0, result, 0, size);
            tempbitmap.UnlockBits(bitData);
            if (temp != bitmap)
                temp.Dispose();
            return new(result);
        }                
        catch
        {                    
            return null;
        }                
    }
}
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

public record class IMG(byte[] Data)
{
    const ImageLockMode Read = ImageLockMode.ReadOnly;
    
    const ImageLockMode Write = ImageLockMode.WriteOnly;
    
    const PixelFormat Format = PixelFormat.Format32bppArgb;
     
    // Start Size Approximation

    private readonly int Size = Data.Length >> 2;
    
    public int Width => GetValue(Size);

    public int Height => GetValue((double)Size / Width);
    
    private static int GetValue(double x) => (int)Math.Ceiling(x);
    
    private static int GetValue(int x) => (int)Math.Ceiling(Math.Sqrt(x));
    
    // End Size Approximation
    
    public Bitmap Build()
    {
        var rect = new Rectangle(0, 0, Width, Height);                                
        var result = new Bitmap(Width, Height, Format);                
        var bitData = result.LockBits(rect, Write, Format);
        Marshal.Copy(Data, 0, bitData.Scan0, Data.Length);
        result.UnlockBits(bitData);
        return result;
    }

    [DebuggerStepThrough, DebuggerHidden]
    public static IMG? Load(Bitmap bitmap)
    {
        ArgumentNullException.ThrowIfNull(bitmap);
        try
        {
            var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            Bitmap temp = bitmap;                    
            if (bitmap.PixelFormat != Format)
            {
                temp = new(rect.Width, rect.Height, Format);
                using Graphics graphics = Graphics.FromImage(temp);
                graphics.DrawImageUnscaledAndClipped(bitmap, rect);
            }                    
            var bitData = temp.LockBits(rect, Read, Format);
            var size = bitData.Height * bitData.Stride;
            var result = new byte[size];
            Marshal.Copy(bitData.Scan0, result, 0, size);
            temp.UnlockBits(bitData);
            if (temp != bitmap)
                temp.Dispose();
            return new(result);
        }                
        catch
        {                    
            return null;
        }                
    }
}
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

public record class IMG(byte[] Data)
{
    const ImageLockMode Read = ImageLockMode.ReadOnly;
    
    const ImageLockMode Write = ImageLockMode.WriteOnly;
    
    const PixelFormat Format = PixelFormat.Format32bppArgb;
     
    // Start Size Approximation

    private readonly int Size = Data.Length >> 2;
    
    public int Width => GetValue(Size);

    public int Height => GetValue((double)Size / Width);
    
    private static int GetValue(double x) => (int)Math.Ceiling(x);
    
    private static int GetValue(int x) => (int)Math.Ceiling(Math.Sqrt(x));
    
    // End Size Approximation
    
    public Bitmap Build()
    {
        var rect = new Rectangle(0, 0, Width, Height);                                
        var result = new Bitmap(Width, Height, Format);                
        var bitData = result.LockBits(rect, Write, Format);
        Marshal.Copy(Data, 0, bitData.Scan0, Data.Length);
        result.UnlockBits(bitData);
        return result;
    }

    [DebuggerStepThrough, DebuggerHidden]
    public static IMG? Load(Bitmap bitmap)
    {
        ArgumentNullException.ThrowIfNull(bitmap);
        if (bitmap.PixelFormat != Format)
            throw new NotSupportedException(nameof(bitmap));
        var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
        var bitData = bitmap.LockBits(rect, Read, Format);
        var size = bitData.Height * bitData.Stride;
        var result = new byte[size];
        Marshal.Copy(bitData.Scan0, result, 0, size);
        bitmap.UnlockBits(bitData);
        return new(result);                
    }
}
Source Link
Short Int
  • 762
  • 2
  • 14
Loading