faq90.htm

来自「C++builder学习资料C++builder」· HTM 代码 · 共 104 行

HTM
104
字号


<HTML>

<HEAD>

   <TITLE>Add JPEG images as resources to the program</TITLE>

   <META NAME="Author" CONTENT="Harold Howe">

</HEAD>

<BODY BGCOLOR="WHITE">



<CENTER>

<TABLE  BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="640">



<TR>

<TD>

<H3> Add JPEG images as resources to the program</H3>

<P> This FAQ combines the FAQ on <A href="faq89.htm">displaying JPEG images</A> with the FAQ on how to

<A href="faq52.htm">add icon, cursor, bitmap, and sound resources to a BCB project</A>. Here is how it works.

</P>

<P>

<B>Step 1</B> - Make a .RH resource header file that looks like this:

<pre>

<font color="navy">// resource.rh</font>

<font color="green">#ifndef RESOURCE_RH</font>

<font color="green">#define RESOURCE_RH</font>



<font color="green">#define ID_JPEG     1000</font>

<font color="green">#endif</font>

</pre>

<P>

<B>Step 2</B> - Make a .RC resource file that lists the JPEG as an <TT>RCDATA</TT> resource type.

<pre>

<font color="green">#include "resource.rh"</font>



ID_JPEG    RCDATA   <font color="blue">"conf99.JPG"</font>

</pre>



<P>

<B>Step 3</B> - Add the .RC file to your project



<P>

<B>Step 4</B> - Place an image on the form somewhere.

<P>



<B>Step 5</B> - Run this code to load the image from resource.

<pre>

<font color="green">#include &lt;jpeg.hpp></font>

<font color="green">#include "main.h"</font>

<font color="green">#include "resource.rh"</font>



<b>...</b>



<b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>Button1Click<b>(</b>TObject <b>*</b>Sender<b>)</b>

<b>{</b>

    <font color="navy">// Find the resource using the resource ID from resource.rh</font>

    HRSRC rsrc <b>=</b> FindResource<b>(</b>HInstance<b>,</b> MAKEINTRESOURCE<b>(</b>ID_JPEG<b>)</b><b>,</b>RT_RCDATA<b>)</b><b>;</b>

    <b>if</b><b>(</b><b>!</b>rsrc<b>)</b>

        <b>return</b><b>;</b>



    <font color="navy">// Load the resource and save its total size. We will need the size</font>

    <font color="navy">// when we read the data.</font>

    DWORD Size <b>=</b> SizeofResource<b>(</b>HInstance <b>,</b> rsrc<b>)</b><b>;</b>

    HGLOBAL MemoryHandle <b>=</b> LoadResource<b>(</b>HInstance<b>,</b>rsrc<b>)</b><b>;</b>

    <b>if</b><b>(</b>MemoryHandle <b>==</b> NULL<b>)</b>

        <b>return</b><b>;</b>



    <font color="navy">// We need to get the bytes out of the resource somehow.</font>

    <font color="navy">// The API function LockResource allows us to do that.</font>

    <font color="navy">// It returns a BYTE pointer to the raw data in the resource</font>

    <font color="navy">// In this case, the resource is a .JPG file.</font>

    BYTE <b>*</b>MemPtr <b>=</b> <b>(</b>BYTE <b>*</b><b>)</b>LockResource<b>(</b>MemoryHandle<b>)</b><b>;</b>



    <font color="navy">// Now the jpeg image is memory is in MemPtr.</font>

    <font color="navy">// Copy from MemPtr into a TMemoryStream</font>

    std<b>:</b><b>:</b>auto_ptr&lt;TMemoryStream<b>></b>stream<b>(</b><b>new</b> TMemoryStream<b>)</b><b>;</b>

    stream<b>-></b>Write<b>(</b>MemPtr<b>,</b> Size<b>)</b><b>;</b>

    stream<b>-></b>Position <b>=</b> <font color="blue">0</font><b>;</b>



    <font color="navy">// Create a TJPEGImage, and tell it to load from the memory stream.</font>

    std<b>:</b><b>:</b>auto_ptr&lt;TJPEGImage<b>></b> JImage<b>(</b><b>new</b> TJPEGImage<b>(</b><b>)</b><b>)</b><b>;</b>

    JImage<b>-></b>LoadFromStream<b>(</b>stream<b>.</b>get<b>(</b><b>)</b><b>)</b><b>;</b>



    <font color="navy">// Now that we have a TJEGImage, we can use Assign to</font>

    <font color="navy">// copy that image into a TImage.</font>

    MyImage<b>-></b>Width  <b>=</b> JImage<b>-></b>Width<b>;</b>

    MyImage<b>-></b>Height <b>=</b> JImage<b>-></b>Height<b>;</b>

    MyImage<b>-></b>Picture<b>-></b>Assign<b>(</b>JImage<b>.</b>get<b>(</b><b>)</b><b>)</b><b>;</b>

<b>}</b>

</pre>

</p>



<BR>

<TABLE  BORDER=1 CELLPADDING=10 CELLSPACING=0 WIDTH="100%">

<TR> <TD colspan = 2><B>Downloads for this FAQ</B> </TD> </TR>

<TR> <TD><TT><A HREF="download/faq90.zip" >faq90.zip </A></TT></TD><TD>Source code for this FAQ.</TD> </TR>

</TABLE>







</TD> </TR>



</TABLE>

</CENTER>

</BODY>

</HTML>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?