Пример #1
0
        public Bitmap GetBitmap()
        {
            uEye.Defines.Status statusRet = 0;

            // Get last image memory
            Int32 s32LastMemId;
            Int32 s32Width;
            Int32 s32Height;

            statusRet = m_Camera.Memory.GetLast(out s32LastMemId);
            statusRet = m_Camera.Memory.Lock(s32LastMemId);
            statusRet = m_Camera.Memory.GetSize(s32LastMemId, out s32Width, out s32Height);

            Bitmap MyBitmap;

            statusRet = m_Camera.Memory.ToBitmap(s32LastMemId, out MyBitmap);

            // clone bitmap
            Rectangle cloneRect = new Rectangle(0, 0, s32Width, s32Height);

            System.Drawing.Imaging.PixelFormat format = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
            Bitmap cloneBitmap = MyBitmap.Clone(cloneRect, format);

            // unlock image buffer
            statusRet = m_Camera.Memory.Unlock(s32LastMemId);
            MyBitmap.Dispose();
            return(cloneBitmap);
        }
Пример #2
0
        public static Image ResampleBitmap(Image source, int imageWidth, int imageHeight)
        {
            if (source == null || source.Width == 0 || source.Height == 0)
            {
                return(null);
            }
            if (imageWidth == 0 || imageHeight == 0 || imageWidth == -1 || imageHeight == -1)
            {
                return(new Bitmap(source));
            }
            float k  = (float)source.Width / (float)imageWidth;
            float k2 = (float)source.Height / (float)imageHeight;
            int   w;
            int   h;

            if (k2 > k)
            {
                w = Math.Max(1, (int)Math.Round((double)((float)source.Width / k2)));
                h = imageHeight;
            }
            else
            {
                w = imageWidth;
                h = Math.Max(1, (int)Math.Round((double)((float)source.Height / k)));
            }
            System.Drawing.Imaging.PixelFormat format = IsIndexedFormat(source.PixelFormat) ? System.Drawing.Imaging.PixelFormat.Format32bppPArgb : source.PixelFormat;
            Bitmap dest = new Bitmap(w, h, format);

            using (Graphics g = Graphics.FromImage(dest)) {
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                g.DrawImage(source, 0, 0, w, h);
            }
            return(dest);
        }
Пример #3
0
 /// <summary>
 /// Copy constructor
 /// </summary>
 public Bitmap(Bitmap other)
 {
     _bitmap = other._bitmap;
     _width = other._width;
     _height = other._height;
     _format = other._format;
 }
Пример #4
0
 /// <summary>
 /// Creates a new image with the specified width, height and format.
 ///
 /// Width and height are in pixels.
 /// </summary>
 public Bitmap(uint width, uint height, BitmapPixelFormat format, BitmapImage bmp)
 {
     _bitmap = bmp;
     _width = width;
     _height = height;
     _format = format;
 }
Пример #5
0
 public Bitmap(int width, int height,
               System.Drawing.Imaging.PixelFormat format)
 {
     SetDGImage(new DotGNU.Images.Image
                    (width, height, (DotGNU.Images.PixelFormat)format));
     dgImage.AddFrame();
 }
Пример #6
0
        private int valueOfPixelFormat(System.Drawing.Imaging.PixelFormat pf)
        {
            int result = 0;

            switch (pf)
            {
            case System.Drawing.Imaging.PixelFormat.Format24bppRgb:
                result = 3;
                break;

            case System.Drawing.Imaging.PixelFormat.Format32bppArgb:
                result = 4;
                break;

            case System.Drawing.Imaging.PixelFormat.Format32bppPArgb:
                result = 4;
                break;

            case System.Drawing.Imaging.PixelFormat.Format32bppRgb:
                result = 4;
                break;

            case System.Drawing.Imaging.PixelFormat.Format8bppIndexed:
                result = 3;
                break;

            default:
                throw new ArgumentException("Unsupported pixel format");
            }
            return(result);
        }
Пример #7
0
        public Image RenderImage(ILUT lut)
        {
            bool render = false;

            if (_bitmap == null)
            {
                System.Drawing.Imaging.PixelFormat format = Components == 4
                                        ? System.Drawing.Imaging.PixelFormat.Format32bppArgb
                                        : System.Drawing.Imaging.PixelFormat.Format32bppRgb;
                _pixels = new PinnedIntArray(ScaledData.Width * ScaledData.Height);
                _bitmap = new Bitmap(ScaledData.Width, ScaledData.Height, ScaledData.Width * 4, format, _pixels.Pointer);
                render  = true;
            }
            if (_applyLut && lut != null && !lut.IsValid)
            {
                lut.Recalculate();
                render = true;
            }
            _bitmap.RotateFlip(RotateFlipType.RotateNoneFlipNone);
            if (render)
            {
                ScaledData.Render((_applyLut ? lut : null), _pixels.Data);
            }
            _bitmap.RotateFlip(GetRotateFlipType());
            return(_bitmap);
        }
Пример #8
0
        private Bitmap SliceMainFrame(int _a, int _b)
        {
            //0 - 7 (64x128) - LEFT WING
            //8 - 15 (64x128) - RIGHT WING
            //16 - 18 - LOOK UPWARDS
            //19 - 21 - LOOK BACKWARDS
            //22 - 25 - BEFORE/AFTER LOOK UPWARDS
            //26 - 31 - USE MAGIC
            //32 - EMPTY
            //33 - 39 (64x128) - TURN AROUND
            //40 - 79 - EMPTY
            //80 - 158 - TAIL ANIMATION
            //160 - 238 - LOWER BODY ANIMATION
            //159 AND 239 - EMPTY
            Bitmap _MAINFRAMEb = (Bitmap)Var.MAINFRAME;
            int    _size       = 64;

            if ((_a < 16 && _b == 0) || (_a > 32 && _b == 0))
            {
                _size = 128;
            }
            Rectangle _cloneRect = new Rectangle(_a * 64, _b * 64, 64, _size);

            System.Drawing.Imaging.PixelFormat _format = System.Drawing.Imaging.PixelFormat.Format32bppPArgb;
            Bitmap _result = _MAINFRAMEb.Clone(_cloneRect, _format);

            return(_result);
        }
Пример #9
0
    public static Bitmap ToBitmap(this BitmapSource bitmapSource, System.Drawing.Imaging.PixelFormat pixelFormat)
    {
        int width  = bitmapSource.PixelWidth;
        int height = bitmapSource.PixelHeight;
        int stride = width * ((bitmapSource.Format.BitsPerPixel + 7) / 8);  // 行の長さは色深度によらず8の倍数のため
        var intPtr = IntPtr.Zero;

        try
        {
            intPtr = Marshal.AllocCoTaskMem(height * stride);
            bitmapSource.CopyPixels(new Int32Rect(0, 0, width, height), intPtr, height * stride, stride);
            using (var bitmap = new Bitmap(width, height, stride, pixelFormat, intPtr))
            {
                // IntPtrからBitmapを生成した場合、Bitmapが存在する間、AllocCoTaskMemで確保したメモリがロックされたままとなる
                // (FreeCoTaskMemするとエラーとなる)
                // そしてBitmapを単純に開放しても解放されない
                // このため、明示的にFreeCoTaskMemを呼んでおくために一度作成したBitmapから新しくBitmapを
                // 再作成し直しておくとメモリリークを抑えやすい
                return(new Bitmap(bitmap));
            }
        }
        finally
        {
            if (intPtr != IntPtr.Zero)
            {
                Marshal.FreeCoTaskMem(intPtr);
            }
        }
    }
Пример #10
0
        private static System.Windows.Media.PixelFormat ConvertBmpPixelFormat(System.Drawing.Imaging.PixelFormat pixelformat)
        {
            System.Windows.Media.PixelFormat pixelFormats = System.Windows.Media.PixelFormats.Default;

            switch (pixelformat)
            {
            case System.Drawing.Imaging.PixelFormat.Format32bppArgb:
                pixelFormats = PixelFormats.Bgr32;
                break;

            case System.Drawing.Imaging.PixelFormat.Format8bppIndexed:
                pixelFormats = PixelFormats.Gray8;
                break;

            case System.Drawing.Imaging.PixelFormat.Format16bppGrayScale:
                pixelFormats = PixelFormats.Gray16;
                break;

            case System.Drawing.Imaging.PixelFormat.Format64bppArgb:
                pixelFormats = PixelFormats.Rgba64;
                break;

            case System.Drawing.Imaging.PixelFormat.Format32bppRgb:
                pixelFormats = PixelFormats.Bgra32;
                break;
            }

            return(pixelFormats);
        }
Пример #11
0
            /// <summary>
            /// Deserialize image.
            /// </summary>
            /// <param name="reader">Buffer reader being used.</param>
            /// <param name="target">Target image into which to deserialize.</param>
            /// <param name="context">Serialization context.</param>
            public void Deserialize(BufferReader reader, ref Image target, SerializationContext context)
            {
                CompressionMethod methodOfCompression = CompressionMethod.None;

                if (this.schema.Version >= 4)
                {
                    Serializer.Deserialize(reader, ref methodOfCompression, context);
                }

                if (methodOfCompression == CompressionMethod.None)
                {
                    Serializer.Deserialize(reader, ref target.image, context);
                    Serializer.Deserialize(reader, ref target.width, context);
                    Serializer.Deserialize(reader, ref target.height, context);
                    Serializer.Deserialize(reader, ref target.stride, context);
                    if (this.schema.Version <= 2)
                    {
                        System.Drawing.Imaging.PixelFormat pixFmt = default(System.Drawing.Imaging.PixelFormat);
                        Serializer.Deserialize(reader, ref pixFmt, context);
                        target.pixelFormat = PixelFormatHelper.FromSystemPixelFormat(pixFmt);
                    }
                    else
                    {
                        Serializer.Deserialize(reader, ref target.pixelFormat, context);
                    }
                }
                else
                {
                    imageCompressor.Deserialize(reader, ref target, context);
                }
            }
Пример #12
0
        // Lock a region of this bitmap.  Use of this method is discouraged.
        // It assumes that managed arrays are fixed in place in memory,
        // which is true for ilrun, but maybe not other CLR implementations.
        // We also assume that "format" is the same as the bitmap's real format.
        public unsafe BitmapData LockBits
            (Rectangle rect, ImageLockMode flags,
            System.Drawing.Imaging.PixelFormat format)
        {
            BitmapData bitmapData = new BitmapData();

            bitmapData.Width       = rect.Width;
            bitmapData.Height      = rect.Height;
            bitmapData.PixelFormat = format;
            if (dgImage != null)
            {
                Frame frame = dgImage.GetFrame(0);
                if (frame != null)
                {
                    if (format != this.PixelFormat)
                    {
                        frame = frame.Reformat((DotGNU.Images.PixelFormat)format);
                    }
                    bitmapData.Stride = frame.Stride;
                    byte[] data = frame.Data;
                    bitmapData.dataHandle = GCHandle.Alloc(data);

                    int offset = rect.X * GetPixelFormatSize(format) / 8;
                    // TODO: will GCHandle.AddrOfPinnedObject work more
                    //       portably across GCs ?
                    fixed(byte *pixel = &(data[rect.Y * frame.Stride]))
                    {
                        bitmapData.Scan0 = (IntPtr)(void *)(pixel + offset);
                    }
                }
            }
            return(bitmapData);
        }
Пример #13
0
        private void InitGrabber()
        {
            try
            {
                this.pixelFormat = PixelFormat.Format32bppRgb;
                boundsRect       = new System.Drawing.Rectangle(0, 0, WIDTH, HEIGHT);

                var format = SharpDX.Direct3D9.Format.A8R8G8B8;
                SharpDX.Direct3D9.PresentParameters present_params = new SharpDX.Direct3D9.PresentParameters();
                present_params.Windowed         = true;
                present_params.BackBufferFormat = format;
                present_params.SwapEffect       = SharpDX.Direct3D9.SwapEffect.Discard;
                present_params.BackBufferWidth  = WIDTH;
                present_params.BackBufferHeight = HEIGHT;
                dx9Device = new SharpDX.Direct3D9.Device(new SharpDX.Direct3D9.Direct3D(),
                                                         0,
                                                         SharpDX.Direct3D9.DeviceType.Hardware,
                                                         IntPtr.Zero,
                                                         SharpDX.Direct3D9.CreateFlags.HardwareVertexProcessing,
                                                         present_params);

                dx9Device.SetRenderState(RenderState.CullMode, Cull.None);
                dx9Device.SetRenderState(RenderState.Lighting, false);
                dx9Device.SetRenderState(RenderState.AntialiasedLineEnable, false);
            }
            catch (SharpDX.SharpDXException dxe)
            {
                LdpLog.Error("SharpDX InitializeDX9\n" + dxe.Message);
            }
            catch (Exception ex)
            {
                LdpLog.Error("InitializeDX9\n" + ex.Message);
            }
        }
        private GLTexture LoadGLTexture(string name, string filePath)
        {
            int id;

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

            using (var bitmap = new Bitmap(filePath))
            {
                const System.Drawing.Imaging.PixelFormat pixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
                var rectangle = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
                var data      = bitmap.LockBits(rectangle, ImageLockMode.ReadOnly, pixelFormat);

                GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0,
                              OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);

                bitmap.UnlockBits(data);

                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);

                return(new GLTexture(id, name, bitmap.Width, bitmap.Height, filePath));
            }
        }
        public byte GetBitsPerPixel(System.Drawing.Imaging.PixelFormat Pixelformat)
        {
            try
            {
                int BitsPerPixel;

                switch (Pixelformat)
                {
                case System.Drawing.Imaging.PixelFormat.Format8bppIndexed:
                    BitsPerPixel = 8;
                    break;

                case System.Drawing.Imaging.PixelFormat.Format24bppRgb:
                    BitsPerPixel = 24;
                    break;

                case System.Drawing.Imaging.PixelFormat.Format32bppArgb:
                case System.Drawing.Imaging.PixelFormat.Format32bppPArgb:
                    BitsPerPixel = 32;
                    break;

                default:
                    BitsPerPixel = 0;
                    break;
                }

                byte bitsPerPixel = (byte)((float)(BitsPerPixel + 7) / 8);
                return(bitsPerPixel);
            }
            catch (Exception)
            {
                throw;
            }
        }
Пример #16
0
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            BitmapSource bitmapSource = value as BitmapSource;

            if (bitmapSource == null)
            {
                return(null);
            }

            int    width   = bitmapSource.PixelWidth;
            int    height  = bitmapSource.PixelHeight;
            int    stride  = width * ((bitmapSource.Format.BitsPerPixel + 7) / 8);
            IntPtr pointer = IntPtr.Zero;

            try
            {
                System.Drawing.Imaging.PixelFormat pixelFormat = this.GetPixelFormat(bitmapSource.Format);


                pointer = Marshal.AllocHGlobal(height * stride);
                bitmapSource.CopyPixels(new Int32Rect(0, 0, width, height), pointer, height * stride, stride);
                using (var bitmap = new Bitmap(width, height, stride, pixelFormat, pointer))
                {
                    return(new Bitmap(bitmap));
                }
            }
            finally
            {
                if (pointer != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pointer);
                }
            }
        }
Пример #17
0
    public uint instantiate(uint FrameWidth, uint FrameHeight, uint BitDepth, uint Orientation)
    {
        VideoInfoStruct videoInfo;

        videoInfo.FrameWidth  = FrameWidth;
        videoInfo.FrameHeight = FrameHeight;
        videoInfo.BitDepth    = BitDepth;
        switch (BitDepth)
        {
        case (uint)PixelFormat.Format16bppRgb565:
            pixelFormat = System.Drawing.Imaging.PixelFormat.Format16bppRgb565;
            break;

        case (uint)PixelFormat.Format24bppRgb:
            pixelFormat = System.Drawing.Imaging.PixelFormat.Format24bppRgb;
            break;

        case (uint)PixelFormat.Format32bppArgb:
            pixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
            break;

        default:
            pixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
            break;
        }
        videoInfo.Orientation = Orientation;
        IntPtr ptr = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(VideoInfoStruct)));

        Marshal.StructureToPtr(videoInfo, ptr, true);
        uint ret = InvokeFunc(plugMainAddr, 11, (uint)ptr.ToInt32(), 0);

        Marshal.FreeCoTaskMem(ptr);
        return(ret);
    }
        /// <summary>
        /// Converts BitmapSource to Bitmap.
        /// </summary>
        /// <param name="sourceWpf">BitmapSource</param>
        /// <returns>Bitmap</returns>
        private static Bitmap ConvertToBitmap(BitmapSource sourceWpf)
        {
            BitmapSource bmpWpf = sourceWpf;

            // PixelFormat settings/conversion
            System.Drawing.Imaging.PixelFormat formatBmp = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
            if (sourceWpf.Format == PixelFormats.Bgr24)
            {
                formatBmp = System.Drawing.Imaging.PixelFormat.Format24bppRgb;
            }
            else if (sourceWpf.Format == System.Windows.Media.PixelFormats.Pbgra32)
            {
                formatBmp = System.Drawing.Imaging.PixelFormat.Format32bppPArgb;
            }
            else if (sourceWpf.Format != System.Windows.Media.PixelFormats.Bgra32)
            {
                // Convert BitmapSource
                FormatConvertedBitmap convertWpf = new FormatConvertedBitmap();
                convertWpf.BeginInit();
                convertWpf.Source            = sourceWpf;
                convertWpf.DestinationFormat = PixelFormats.Bgra32;
                convertWpf.EndInit();
                bmpWpf = convertWpf;
            }

            // Copy/Convert to Bitmap
            Bitmap     bmp  = new Bitmap(bmpWpf.PixelWidth, bmpWpf.PixelHeight, formatBmp);
            Rectangle  rect = new Rectangle(Point.Empty, bmp.Size);
            BitmapData data = bmp.LockBits(rect, ImageLockMode.WriteOnly, formatBmp);

            bmpWpf.CopyPixels(System.Windows.Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride);
            bmp.UnlockBits(data);

            return(bmp);
        }
Пример #19
0
        public static Bitmap ColorImageFrameToBitmap(ColorImageFrame colorFrame)
        {
            byte[] pixelBuffer = new byte[colorFrame.PixelDataLength];
            colorFrame.CopyPixelDataTo(pixelBuffer);

            System.Drawing.Imaging.PixelFormat pixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppRgb;

            if (colorFrame.Format == ColorImageFormat.InfraredResolution640x480Fps30)
            {
                pixelFormat = System.Drawing.Imaging.PixelFormat.Format16bppRgb565;
            }

            Bitmap bitmapFrame = new Bitmap(colorFrame.Width, colorFrame.Height, pixelFormat);

            System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, bitmapFrame.Width, bitmapFrame.Height);
            BitmapData bitmapData         = bitmapFrame.LockBits(rect, ImageLockMode.WriteOnly, bitmapFrame.PixelFormat);

            IntPtr intPointer = bitmapData.Scan0;

            Marshal.Copy(pixelBuffer, 0, intPointer, colorFrame.PixelDataLength);

            bitmapFrame.UnlockBits(bitmapData);

            bitmapData  = null;
            pixelBuffer = null;

            return(bitmapFrame);
        }
Пример #20
0
		static void ConvertPixelFormat(PixelLayout from, out System.Drawing.Imaging.PixelFormat to, out int flags)
		{
			switch (from) {
				case PixelLayout.R8:
					to = System.Drawing.Imaging.PixelFormat.Format8bppIndexed;
					flags = (int)ImageFlags.ColorSpaceGray;
					break;
				case PixelLayout.R16:
					to = System.Drawing.Imaging.PixelFormat.Format16bppGrayScale;
					flags = (int)ImageFlags.ColorSpaceGray;
					break;
				case PixelLayout.BGR15:
					to = System.Drawing.Imaging.PixelFormat.Format16bppRgb555;
					flags = (int)ImageFlags.ColorSpaceRgb;
					break;
				case PixelLayout.BGR16:
					to = System.Drawing.Imaging.PixelFormat.Format16bppRgb565;
					flags = (int)ImageFlags.ColorSpaceRgb;
					break;
				case PixelLayout.BGR24:
					to = System.Drawing.Imaging.PixelFormat.Format24bppRgb;
					flags = (int)ImageFlags.ColorSpaceRgb;
					break;
				case PixelLayout.BGRA32:
					to = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
					flags = (int)ImageFlags.ColorSpaceRgb;
					break;
				default:
					throw new ArgumentException(String.Format("pixel format {0} not supported", from));
			}
		}
Пример #21
0
        internal static Bitmap FromStream(Stream stream)
        {
            Bitmap bitmap = null;

            Task.Run(async() =>
            {
                using (var raStream = new InMemoryRandomAccessStream())
                {
                    await stream.CopyToAsync(raStream.AsStream());
                    var decoder   = await BitmapDecoder.CreateAsync(raStream);
                    var pixelData = await decoder.GetPixelDataAsync();

                    var width  = (int)decoder.OrientedPixelWidth;
                    var height = (int)decoder.OrientedPixelHeight;
                    const PixelFormat format = PixelFormat.Format32bppArgb;
                    var bytes = pixelData.DetachPixelData();

                    bitmap   = new Bitmap(width, height, format);
                    var data = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, format);
                    Marshal.Copy(bytes, 0, data.Scan0, bytes.Length);
                    bitmap.UnlockBits(data);
                }
            }).Wait();

            return(bitmap);
        }
Пример #22
0
        private void GetMediaType(System.Drawing.Imaging.PixelFormat pixelFormat, out Guid mediaSubType, out short bitCount)
        {
            switch (pixelFormat)
            {
            case System.Drawing.Imaging.PixelFormat.Format32bppArgb:
                mediaSubType = MediaSubType.RGB32;
                bitCount     = 32;
                break;

            case System.Drawing.Imaging.PixelFormat.Format32bppRgb:
                mediaSubType = MediaSubType.RGB32;
                bitCount     = 32;
                break;

            case System.Drawing.Imaging.PixelFormat.Format24bppRgb:
                mediaSubType = MediaSubType.RGB24;
                bitCount     = 24;
                break;

            case System.Drawing.Imaging.PixelFormat.Format16bppRgb565:
                mediaSubType = MediaSubType.RGB565;
                bitCount     = 16;
                break;

            default:
                throw new Exception("Unrecognized Pixelformat in bitmap");
            }
        }
Пример #23
0
        public Bitmap CopyToBitmap(int width, int height, ImagingPixelFormat pixelFormat)
        {
            if (width <= 0)
            {
                throw new ArgumentOutOfRangeException("width",
                                                      "width must be greater than zero.");
            }

            if (height <= 0)
            {
                throw new ArgumentOutOfRangeException("height",
                                                      "height must be greater than zero.");
            }

            Bitmap bitmap = new Bitmap(width, height, pixelFormat);

            BitmapData lockedPixels = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                                                      ImageLockMode.WriteOnly, bitmap.PixelFormat);

            int sizeInBytes = lockedPixels.Stride * lockedPixels.Height;

            Bind();
            GL.GetBufferSubData(_type, new IntPtr(), new IntPtr(sizeInBytes), lockedPixels.Scan0);

            bitmap.UnlockBits(lockedPixels);

            //
            // OpenGL had rows bottom to top.  Bitmap wants them top to bottom.
            //
            bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);

            return(bitmap);
        }
        public static Bitmap ToBitmap(VideoFrame frame)
        {
            var bytes = new byte[frame.Stride * frame.Height];

            frame.CopyTo <byte>(bytes);

            var         bpp = frame.BitsPerPixel;
            PixelFormat pf  = PixelFormat.Format24bppRgb;

            switch (bpp)
            {
            case 16:
                pf = PixelFormat.Format16bppGrayScale;
                break;

            case 24:
                pf = PixelFormat.Format24bppRgb;
                break;

            case 32:
                pf = PixelFormat.Format32bppArgb;
                break;
            }

            var        bs         = new Bitmap(frame.Width, frame.Height, pf);
            var        BoundsRect = new System.Drawing.Rectangle(0, 0, frame.Width, frame.Height);
            BitmapData bmpData    = bs.LockBits(BoundsRect, ImageLockMode.WriteOnly, pf);

            System.Runtime.InteropServices.Marshal.Copy(bytes, 0, bmpData.Scan0, frame.Stride * frame.Height);
            bs.UnlockBits(bmpData);

            return(bs);
        }
Пример #25
0
 /// <summary>
 /// Saves to file.
 /// </summary>
 /// <param name="texture">The texture.</param>
 /// <param name="fileName">Name of the file.</param>
 /// <param name="format">The format.</param>
 public static void SaveToFile(this ITexture2D texture, string fileName, SysDraw.PixelFormat format = SysDraw.PixelFormat.Format32bppArgb)
 {
     using (var bitmap = SaveToBitmap(texture, format))
     {
         bitmap.Save(fileName);
     }
 }
Пример #26
0
        public static int BitsPerPixel(this sdi.PixelFormat format)
        {
            switch (format)
            {
            case sdi.PixelFormat.Format1bppIndexed:
                return(1);

            case sdi.PixelFormat.Format4bppIndexed:
                return(4);

            case sdi.PixelFormat.Format8bppIndexed:
                return(8);

            case sdi.PixelFormat.Format24bppRgb:
                return(24);

            case sdi.PixelFormat.Format32bppArgb:
            case sdi.PixelFormat.Format32bppPArgb:
            case sdi.PixelFormat.Format32bppRgb:
                return(32);

            default:
                throw new NotSupportedException();
            }
        }
Пример #27
0
        public Bitmap GetPicThumbnail(Bitmap iSource, int dHeight, int dWidth, System.Drawing.Imaging.PixelFormat flag, int initHeight, int intFromTop)
        {
            System.Drawing.Image initImage = iSource;

            //原图宽高均小于模版,不作处理,直接保存
            if (initImage.Width <= dWidth && initImage.Height <= dHeight)
            {
                return(iSource);
            }
            else
            {
                //原始图片的宽
                int initWidth = initImage.Width;
                //截图对象
                System.Drawing.Image    pickedImage = null;
                System.Drawing.Graphics pickedG     = null;

                pickedImage = new System.Drawing.Bitmap(initWidth, initHeight);
                pickedG     = System.Drawing.Graphics.FromImage(pickedImage);
                //设置质量
                pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                pickedG.SmoothingMode     = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                //定位
                Rectangle fromR = new Rectangle(0, intFromTop, initWidth, initHeight);
                Rectangle toR   = new Rectangle(0, 0, initWidth, initHeight);
                //画图
                pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);

                //将截图对象赋给原图
                initImage = (System.Drawing.Image)pickedImage.Clone();
                //释放截图资源
                pickedG.Dispose();
                pickedImage.Dispose();

                //缩略图对象
                System.Drawing.Image    resultImage = new System.Drawing.Bitmap(dWidth, dHeight);
                System.Drawing.Graphics resultG     = System.Drawing.Graphics.FromImage(resultImage);
                //设置质量
                resultG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                resultG.SmoothingMode     = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                //用指定背景色清空画布
                resultG.Clear(Color.White);
                //绘制缩略图
                resultG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, dWidth, dHeight), new System.Drawing.Rectangle(0, 0, initWidth, initHeight), System.Drawing.GraphicsUnit.Pixel);
                //关键质量控制
                //获取系统编码类型数组,包含了jpeg,bmp,png,gif,tiff
                ImageCodecInfo[] icis = ImageCodecInfo.GetImageEncoders();
                ImageCodecInfo   ici  = null;
                foreach (ImageCodecInfo i in icis)
                {
                    if (i.MimeType == "image/jpeg" || i.MimeType == "image/bmp" || i.MimeType == "image/png" || i.MimeType == "image/gif")
                    {
                        ici = i;
                    }
                }
                EncoderParameters ep = new EncoderParameters(1);
                ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)flag);
                return((Bitmap)resultImage);
            }
        }
Пример #28
0
 /// <summary>
 /// Creates a new image with the specified width, height and format.
 ///
 /// Width and height are in pixels.
 /// </summary>
 public Bitmap(uint width, uint height, BitmapPixelFormat format, BitmapImage bmp)
 {
     _bitmap = bmp;
     _width  = width;
     _height = height;
     _format = format;
 }
Пример #29
0
        private static unsafe Bitmap CreateAlphaBitmap(Bitmap srcBitmap, PixelFormat targetPixelFormat)
        {
            var result = new Bitmap(srcBitmap.Width, srcBitmap.Height, targetPixelFormat);

            var bmpBounds = new Rectangle(0, 0, srcBitmap.Width, srcBitmap.Height);
            var srcData   = srcBitmap.LockBits(bmpBounds, ImageLockMode.ReadOnly, srcBitmap.PixelFormat);
            var destData  = result.LockBits(bmpBounds, ImageLockMode.ReadOnly, targetPixelFormat);

            var srcDataPtr  = (byte *)srcData.Scan0;
            var destDataPtr = (byte *)destData.Scan0;

            try
            {
                for (var y = 0; y <= srcData.Height - 1; y++)
                {
                    for (var x = 0; x <= srcData.Width - 1; x++)
                    {
                        //this is really important because one stride may be positive and the other negative
                        var position  = srcData.Stride * y + 4 * x;
                        var position2 = destData.Stride * y + 4 * x;

                        memcpy(destDataPtr + position2, srcDataPtr + position, (UIntPtr)4);
                    }
                }
            }
            finally
            {
                srcBitmap.UnlockBits(srcData);
                result.UnlockBits(destData);
            }

            return(result);
        }
Пример #30
0
        private Image FrameDraw(Image img)
        {
            int      max = img.Height;
            int      min = img.Height / 8;
            Graphics gp  = Graphics.FromImage(img);
            Bitmap   bmp = new Bitmap(img);

            System.Drawing.Imaging.PixelFormat Format = img.PixelFormat;
            for (int i = max; i >= min; i = i / 2)
            {
                for (int x = 0; x < img.Width; x += i)
                {
                    for (int y = 0; y < max; y += i)
                    {
                        Rectangle rect  = new Rectangle(0, 0, i, i);
                        Bitmap    clone = bmp.Clone(rect, Format);
                        Bitmap32  bmp32 = new Bitmap32(clone);
                        bmp32.LockBitmap();
                        bmp32.Robert(r, g, b);
                        bmp32.UnlockBitmap();
                    }
                }
            }
            return(img);
        }
Пример #31
0
 private static void CheckPixelFormat(System.Drawing.Imaging.PixelFormat pixelFormat)
 {
     if (pixelFormat != System.Drawing.Imaging.PixelFormat.Format16bppGrayScale)
     {
         throw new InvalidOperationException(
                   $"Depth images can only be constructed from bitmaps with {nameof(System.Drawing.Imaging.PixelFormat.Format16bppGrayScale)} format.");
     }
 }
 public UnmanagedImage(IntPtr imageData, int width, int height, int stride, System.Drawing.Imaging.PixelFormat pixelFormat)
 {
     this.imageData   = imageData;
     this.width       = width;
     this.height      = height;
     this.stride      = stride;
     this.pixelFormat = pixelFormat;
 }
Пример #33
0
        public static void SaveMapAsImage(PixelFormat mapPiFormat, int mapLevel)
        {
            var frm = new FrmMapDownloader
                {
                    _workMode = WorkMode.SaveToImageFile,
                    _mapPiFormat = mapPiFormat,
                    _mapLevel = mapLevel,
                    Text = @"Save Map As Image",
                    labelInfo = {Text = @"Save big map as one image"}
                };

            frm.ShowDialog();
        }
Пример #34
0
        private void DisplayImage(int width, int height, float dpiX, float dpiY, WinFormsPixelFormat pixelFormat,
			byte[] imageData)
        {
            try
            {
                var wpfPixelFormat = getWpfPixelFormatFromWinformsPixelFormat(pixelFormat);

                var bitsPerPixel = wpfPixelFormat.BitsPerPixel;
                var stride = ((width*bitsPerPixel + (bitsPerPixel - 1)) & ~(bitsPerPixel - 1))/8;

                Parallel.For(0, imageData.Length / 3, index =>
                    {
                        int i = index*3;
                        byte r = imageData[i + 0];
                        byte g = imageData[i + 1];
                        byte b = imageData[i + 2];

                        //var yCbCr = new YCbCr(r / 255.0f, g / 255.0f, b / 255.0f);
                        //var rgb = yCbCr.ToRGB();

                        imageData[i] = r;
                        imageData[i + 1] = g;
                        imageData[i + 2] = b;//(byte)(255 - b);
                    }
                );

                var bitmapSource = BitmapSource.Create(
                    width,
                    height,
                    dpiX,
                    dpiY,
                    wpfPixelFormat,
                    null,
                    imageData,
                    stride);

                rawRGB.Width = bitmapSource.Width;
                rawRGB.Height = bitmapSource.Height;
                rawRGB.Source = bitmapSource;
            }
            catch (TaskCanceledException) {}
        }
        private void InitGrabber()
        {
            try
            {
                this.pixelFormat = PixelFormat.Format32bppRgb;
                boundsRect = new System.Drawing.Rectangle(0, 0, WIDTH, HEIGHT);

                uint numAdapter = 0;   // # of graphics card adapter
                uint numOutput = 0;    // # of output device (i.e. monitor)

                // create device and factory
                dx11Device = new SharpDX.Direct3D11.Device(SharpDX.Direct3D.DriverType.Hardware);
                dx11Factory = new Factory1();
                dx11Output = new Output1(dx11Factory.Adapters1[numAdapter].Outputs[numOutput].NativePointer);

                // creating CPU-accessible texture resource
                dx11Texture2Ddescr = new SharpDX.Direct3D11.Texture2DDescription();
                dx11Texture2Ddescr.CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.Read;
                dx11Texture2Ddescr.BindFlags = SharpDX.Direct3D11.BindFlags.None;
                dx11Texture2Ddescr.Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm;
                dx11Texture2Ddescr.Height = HEIGHT;
                dx11Texture2Ddescr.Width = WIDTH;
                dx11Texture2Ddescr.OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None;
                dx11Texture2Ddescr.MipLevels = 1;
                dx11Texture2Ddescr.ArraySize = 1;
                dx11Texture2Ddescr.SampleDescription.Count = 1;
                dx11Texture2Ddescr.SampleDescription.Quality = 0;
                dx11Texture2Ddescr.Usage = SharpDX.Direct3D11.ResourceUsage.Staging;
                dx11ScreenTexture = new SharpDX.Direct3D11.Texture2D(dx11Device, dx11Texture2Ddescr);

                // duplicate output stuff

                dx11DuplicatedOutput = dx11Output.DuplicateOutput(dx11Device);
            }
            catch (SharpDX.SharpDXException dxe)
            {
                string error = "Directx 11 initializer error.\n" + dxe.Message;
                LdpLog.Error(error);
                MessageBox.Show(error, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            catch (Exception ex)
            {
                string error = "Directx 11 initializer error.\n" + ex.Message;
                LdpLog.Error(error);
                MessageBox.Show(error, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Пример #36
0
 public PixelFormatMap(Imaging.PixelFormat Format, OpenGL.PixelFormat GLFormat, OpenGL.PixelInternalFormat numbytes)
 {
     this.PixelFormat = Format;
     this.OpenGLPixelFormat = GLFormat;
     this.InternalFormat = numbytes;
 }
Пример #37
0
	// Set the dgImage field within this object.
	internal void SetDGImage(DotGNU.Images.Image dgImage)
			{
				flags = 0;
			#if !ECMA_COMPAT
				switch(dgImage.LoadFormat)
				{
					case DotGNU.Images.Image.Png:
						rawFormat = ImageFormat.Png; break;
					case DotGNU.Images.Image.Jpeg:
						rawFormat = ImageFormat.Jpeg; break;
					case DotGNU.Images.Image.Gif:
						rawFormat = ImageFormat.Gif; break;
					case DotGNU.Images.Image.Tiff:
						rawFormat = ImageFormat.Tiff; break;
					case DotGNU.Images.Image.Bmp:
						rawFormat = ImageFormat.Bmp; break;
					case DotGNU.Images.Image.Icon:
						rawFormat = ImageFormat.Icon; break;
					case DotGNU.Images.Image.Exif:
						rawFormat = ImageFormat.Exif; break;
				}
				frameDimensionsList = new Guid [0];
				this.dgImage = dgImage;
				// If we are loading an icon, set the size of the image
				// to the size of the first icon
				if (rawFormat == ImageFormat.Icon)
				{
					width = dgImage.GetFrame(0).Width;
					height = dgImage.GetFrame(0).Height;
				}
				else
				{
					width = dgImage.Width;
					height = dgImage.Height;
				}
			#else
				this.dgImage = dgImage;
				width = dgImage.GetFrame(0).Width;
				height = dgImage.GetFrame(0).Height;
			#endif
				horizontalResolution = Graphics.DefaultScreenDpi;
				verticalResolution = Graphics.DefaultScreenDpi;
				pixelFormat = (System.Drawing.Imaging.PixelFormat)
					(dgImage.PixelFormat);
			}
Пример #38
0
 private PixelFormat getWpfPixelFormatFromWinformsPixelFormat(WinFormsPixelFormat pixelFormat)
 {
     switch (pixelFormat)
     {
         case WinFormsPixelFormat.Format24bppRgb:
             return PixelFormats.Rgb24;
         default:
             throw new InvalidCastException($"No mapping created for winforms pixel format {pixelFormat}");
     }
 }
Пример #39
0
	public uint instantiate(uint FrameWidth, uint FrameHeight, uint BitDepth, uint Orientation)
	{
		VideoInfoStruct videoInfo;
		videoInfo.FrameWidth = FrameWidth;
		videoInfo.FrameHeight = FrameHeight;
		videoInfo.BitDepth = BitDepth;
		switch(BitDepth)
		{
			case (uint) PixelFormat.Format16bppRgb565:
				pixelFormat = System.Drawing.Imaging.PixelFormat.Format16bppRgb565;
				break;
			case (uint) PixelFormat.Format24bppRgb:
				pixelFormat = System.Drawing.Imaging.PixelFormat.Format24bppRgb;
				break;
			case (uint) PixelFormat.Format32bppArgb:
				pixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
				break;
			default:
				pixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
				break;
		}
		videoInfo.Orientation = Orientation;
		IntPtr ptr = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(VideoInfoStruct)));
		Marshal.StructureToPtr(videoInfo, ptr, true);
		uint ret = InvokeFunc(plugMainAddr, 11,  (uint) ptr.ToInt32(), 0);
		Marshal.FreeCoTaskMem(ptr);
		return ret;
	}
        private void InitGrabber()
        {
            try
            {
                this.pixelFormat = PixelFormat.Format32bppRgb;
                boundsRect = new System.Drawing.Rectangle(0, 0, WIDTH, HEIGHT);

                var format = SharpDX.Direct3D9.Format.A8R8G8B8;
                SharpDX.Direct3D9.PresentParameters present_params = new SharpDX.Direct3D9.PresentParameters();
                present_params.Windowed = true;
                present_params.BackBufferFormat = format;
                present_params.SwapEffect = SharpDX.Direct3D9.SwapEffect.Discard;
                present_params.BackBufferWidth = WIDTH;
                present_params.BackBufferHeight = HEIGHT;
                dx9Device = new SharpDX.Direct3D9.Device(new SharpDX.Direct3D9.Direct3D(),
                                                      0,
                                                      SharpDX.Direct3D9.DeviceType.Hardware,
                                                      IntPtr.Zero,
                                                      SharpDX.Direct3D9.CreateFlags.HardwareVertexProcessing,
                                                      present_params);

                dx9Device.SetRenderState(RenderState.CullMode, Cull.None);
                dx9Device.SetRenderState(RenderState.Lighting, false);
                dx9Device.SetRenderState(RenderState.AntialiasedLineEnable, false);
            }
            catch (SharpDX.SharpDXException dxe)
            {
                LdpLog.Error("SharpDX InitializeDX9\n" + dxe.Message);
            }
            catch (Exception ex)
            {
                LdpLog.Error("InitializeDX9\n" + ex.Message);
            }
        }