Image processing series tutorial 2: Acquisition of image data in the GDI + version

Source: Internet
Author: User

GDI + is a major improvement of GDI. Its design philosophy and operation methods are essentially different from those of GDI, because of its excellent anti-tooth feature, many interface functions are favored by image and graphic programmers. Compared with GDI, it is quite different in programming.

In some column functions of GDI +, there are two special functions: gdiplusstartup and gdiplusshutdown. Before executing all the functions of GDI + graphics or images, you must call the gdiplusstartup function first, otherwise, the error gdiplusnotinitialized will be returned. Before the program is closed, you must call gdiplusshutdown. Otherwise, if you are in the vb ide, the VB ide should be closed occasionally, in this case, even if you do not care about it, once you input Chinese or copy operations in VB, VB will be suspended, and no buttons in VB can be clicked.

Compared with GDI, the support for image formats by GDI + is greatly extended. in VB6.0, transparent images, such as PNG, are not supported. This is a historical reason, when VB6.0 was released, the PNG format was not popular. The formats that can be opened by GDI + include BMP, JPEG, GIF, PNG, And Tiff. gif and Tiff can contain multiple frames. Similarly, GDI + supports saving the above format, but it cannot save multiple frames of GIF, which may be related to the copyright of GIF.

Calling GDI + in VB is also a simple task. There is already a complete flat-layer GDI + API download on the network. You cannot find a link or contact me directly if necessary.

Let's talk about how to use GDI + to obtain image data.

First, we need to open an image. This function in GDI + has a gdiploadimagefromfile function. We often see two versions of the Declaration:

Private Declare Function GdipLoadImageFromFile Lib "gdiplus" (ByVal FileName As String, Image As Long) As LongPrivate Declare Function GdipLoadImageFromFile Lib "gdiplus" (ByVal FileName As Long, hImage As Long) As Long

In the first version, filename cannot use our path directly, but strconv (filename, vbunicode). Otherwise, the call will fail, and strptr (filename) will be used in the second version ).

If the call is successful, the image value is not 0.

If the image is not 0, it indicates that the call is successful. If you need to display it, You can directly call gdipdrawimage or other overload functions.

To obtain the image data for further processing, we need to find a suitable function. In GDI +, there is also a getpixel function similar to GDI, that is, gdipbitmapgetpixel, which is also not suitable for image processing, then we searched for a function such as gdipbitmaplockbits/gdipbitmapunlockbits in the platform-based API of GDI +. Its declaration is as follows:

Public Declare Function GdipBitmapLockBits Lib "gdiplus" (ByVal bitmap As Long, rect As RECTL, ByVal flags As ImageLockMode, ByVal PixelFormat As Long, lockedBitmapData As BitmapData) As longPublic Declare Function GdipBitmapUnlockBits Lib "gdiplus" (ByVal bitmap As Long, lockedBitmapData As BitmapData) As long

Let's first look at the bitmapdata structure.

Public Type BitmapData   Width As Long   Height As Long   stride As Long   PixelFormat As Long   scan0 As Long   Reserved As LongEnd Type

View msdn to see that width and height indicate the width and height of the image, stride indicates the width of the scanned row of the image, pixelformat indicates the image format, and scan0 indicates the memory address of the image. This scan0 is really a happy thing, a worrying thing. We are glad that we have found the image data, but we are worried that it is a pointer, while VB has no pointer. In an article I published a long time ago, the quick acquisition of color image data in VB.net uses marshal. COPY Copies the data to a temporary array, because it is in VB. net is really unable to use pointers, and using this method will result in slow speed, because the image will be copied back after data processing, and the second is to occupy more than twice the memory. Fortunately, in VB6.0, we can use the pointer simulation method to directly access the memory. For details, see pointer in VB in image processing Series 3.

After calling gdipbitmaplockbits, if the call is successful, the related information will be filled in the lockedbitmapdata struct. Similar to the getdibits function, this function can also set the format to be obtained, that is, the pixelformat parameter. However, because of the different starting points of GDI +, it will dynamically load the image, that is, calling the gdiploadimagefromfile function does not allocate the corresponding memory for the image, therefore, we strongly recommend that the pixelformat here be consistent with the actual image format, which can be obtained through gdipgetimagepixelformat.

As for the rectl parameter, it indicates the rectangular area of the data to be obtained. If you only need to obtain part of the data, you can change it here. Generally, we need to obtain the entire data of the image, there are several functions available here ., As follows:

Gdipgetimagewidth/gdipgetimageheight or gdipgetimagedimension

After processing the image data, similar to setdibits, we need to call gdipbitmapunlockbits to update the image.

In addition to using gdiploadimagefromfile to create image objects of GDI +, you can also use gdipcreatebitmapfromgdib to create Dib objects of GDI and use gdipcreatebitmapfromhbitmap to create image objects from stdpicture objects, using gdipcreatebitmapfromhicon's icon handle and gdipcreatebitmapfromscan0 to create an image from the first address of the bitmap in the memory and other objects, you can easily convert the graphic objects of GDI to GDI +.

There is no DC attribute in GDI + and it is replaced by a graphics object. To draw an image, you must first create graphics. The gdipcreatefromhdc function can convert the DC to graphics, after calling the drawing function, remember to delete gdipdeletegraphics.

Similarly, if you do not use an image object, you also need to call gdipdisposeimage to release it.

 

See: http://www.hellocpp.net/Articles/Article/457.aspx for code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.