yucad3d

Extract a preview bitmap

Save the drawing, then extract a 246×190 Windows-BMP thumbnail into a caller buffer (CadExtractImage, 250 000-byte contract).

// Drawing-preview extraction: save to a temp DXF, extract a 246x190 BMP.
static void feat_extract() {
    char dir[MAX_PATH]; GetTempPathA(MAX_PATH, dir);
    const std::string dxf = std::string(dir) + "yucad_preview_src.dxf";
    const std::string bmp = std::string(dir) + "yucad_preview.bmp";
    if (!CadFileSaveAs(g_hDwg, samp_wnd(), dxf.c_str())) { set_status(L"Save for preview FAILED"); return; }
    static unsigned char buf[250000];                    // Vecad contract: 250 000 bytes
    const unsigned n = CadExtractImage(dxf.c_str(), buf);
    if (!n) { set_status(L"CadExtractImage FAILED"); return; }
    FILE* f = nullptr; fopen_s(&f, bmp.c_str(), "wb");
    if (f) { fwrite(buf, 1, n, f); fclose(f); }
    wchar_t s[300]; swprintf(s, 300, L"CadExtractImage: %u-byte BMP preview -> %hs", n, bmp.c_str());
    set_status(s);
    ShellExecuteA(nullptr, "open", bmp.c_str(), nullptr, nullptr, SW_SHOWNORMAL);
}
    12:                                           // Preview extract (BMP)
      begin
        GetTempPath(MAX_PATH, tmp);
        dxf := string(tmp) + 'yucad_preview_src.dxf';
        bmp := string(tmp) + 'yucad_preview.bmp';
        if CadFileSaveAs(FhDwg, FCadHwnd, PAnsiChar(AnsiString(dxf))) = 0 then
        begin StatusBar.SimpleText := 'Save for preview FAILED'; Exit; end;
        SetLength(buf, 250000);                   // Vecad contract: 250 000 bytes
        cnt := CadExtractImage(PAnsiChar(AnsiString(dxf)), @buf[0]);
        if cnt = 0 then begin StatusBar.SimpleText := 'CadExtractImage FAILED'; Exit; end;
        fs := TFileStream.Create(bmp, fmCreate);
        try fs.WriteBuffer(buf[0], cnt); finally fs.Free; end;
    private void FeatExtract()
    {
        var dxf = Path.Combine(Path.GetTempPath(), "yucad_preview_src.dxf");
        var bmp = Path.Combine(Path.GetTempPath(), "yucad_preview.bmp");
        if (YuCadApi.CadFileSaveAs(_hDwg, Handle, dxf) == 0) { SetStatus("Save for preview FAILED"); return; }
        var buf = new byte[250000];                       // Vecad contract: 250 000 bytes
        var n = YuCadApi.CadExtractImage(dxf, buf);
        if (n == 0) { SetStatus("CadExtractImage FAILED"); return; }
        using (var fs = File.Create(bmp)) fs.Write(buf, 0, (int)n);
        SetStatus($"CadExtractImage: {n}-byte BMP preview -> {bmp}");
        Process.Start(new ProcessStartInfo(bmp) { UseShellExecute = true });
    }
This code is extracted verbatim from the shipped demo hosts (examples/cpp, examples/delphi, examples/csharp) — run the matching menu item there to see it live.