Hi all, recently I tried to add cover art to flac files and have dfficulties in doing so.<div><br></div><div>FLAC__Metadata_Chain *chain = FLAC__metadata_chain_new();</div><div><br></div><div>if(0 == chain)</div><div>     return;</div>
<div><br></div><div>if(!FLAC__metadata_chain_read(chain, filename)) </div><div><span class="Apple-tab-span" style="white-space:pre">        </span>return;</div><div><br clear="all"><div>FLAC__StreamMetadata *picture = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PICTURE);</div>
<div>FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();</div><div><br></div><div>if(0 == iterator)</div><div>      return;</div><div><br></div><div>FLAC__metadata_iterator_init(iterator, chain);</div><div>
while(FLAC__metadata_iterator_next(iterator))</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>;</div><div><br></div><div><div>const char *violation;</div><div>if( !FLAC__metadata_object_picture_is_legal(picture, &amp;violation)) </div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>printf( &quot;Illegal Picture:   %s&quot;, violation );</div><div><br></div><div>if(!FLAC__metadata_iterator_insert_block_after(iterator, picture)) </div><div>
{</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>printf(L&quot; ERROR: adding new PICTURE block to metadata&quot;);</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>FLAC__metadata_object_delete(picture);</div>
<div>}</div><div><br></div><div>FLAC__metadata_iterator_delete(iterator);</div></div><div><br></div><div><div>FLAC__metadata_chain_sort_padding(chain);</div><div>if(!FLAC__metadata_chain_write(chain, true, false ))</div><div>
{</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>const FLAC__Metadata_ChainStatus status = FLAC__metadata_chain_status(chain);</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>printf( &quot;Fail to write:   %d&quot;, status );</div>
<div>}</div><div><br></div><div>FLAC__metadata_chain_delete(chain);</div><div><br></div><div>The above code will add a NULL cover art into the flac file, it works.</div><div><br></div><div>However, after I add the following code between the call of FLAC__metadata_iterator_next  and  FLAC__metadata_object_picture_is_legal,</div>
<div>I got a &quot;fail to write: status 0&quot; message.</div><div><br></div><div>picture-&gt;data.picture.type = (FLAC__StreamMetadata_Picture_Type)3;</div><div>FLAC__metadata_object_picture_set_mime_type( picture, &quot;image/jpeg&quot;, true );</div>
<div>FLAC__metadata_object_picture_set_description( picture, (FLAC__byte*)&quot;&quot;, true );</div><div><br></div><div><div>CFile picfile;</div><div>picfile.Open( L&quot;I:\\1.jpg&quot;, CFile::modeRead | CFile::shareDenyWrite);</div>
<div>ULONGLONG dwLength = picfile.GetLength();</div><div>LPSTR pData = new char[static_cast&lt;unsigned int&gt;(dwLength+1)];</div><div>picfile.Read(pData, static_cast&lt;UINT&gt;(dwLength));</div><div><br></div><div>FLAC__metadata_object_picture_set_data( picture, (FLAC__byte*)pData, </div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>dwLength, true );</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>delete [] pData;</div><div><br></div><div>I did some tests and it seemed that the error was caused by the FLAC__metadata_object_picture_set_data function,</div>
<div>the members of picture object like width, height, depth, colors were not initialized, are they mandatory or optional?</div><div><br></div><div>What&#39;s wrong with the code? </div><div>Please help!</div><div><br></div>
</div></div>-- <br>Best Regards<br>
</div>