save icon to local system

PDF-XChange Editor SDK for Developers

Moderators: TrackerSupp-Daniel, Tracker Support, Paul - Tracker Supp, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Ivan - Tracker Software, Tracker Supp-Stefan

Forum rules
DO NOT post your license/serial key, or your activation code - these forums, and all posts within, are public and we will be forced to immediately deactivate your license.

When experiencing some errors, use the IAUX_Inst::FormatHRESULT method to see their description and include it in your post along with the error code.
HomerWu
User
Posts: 91
Joined: Fri Nov 25, 2016 8:19 am

save icon to local system

Post by HomerWu »

Dear support,
I know we can call method 'GetIcon' to get a IUIX_Icon object,but i wonder if there a way to save the IUIX_Icon object to local system,could you please give me some advice?It would be nice if you could give me some sample code,thanks!
HomerWu
User
Posts: 91
Joined: Fri Nov 25, 2016 8:19 am

Re: save icon to local system

Post by HomerWu »

Dear Support,

I have tried the code below,but failed,the application throws a exception(Attempt to read or write to protected memory) when "System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, (rc.right - rc.left) * 4);",any one can help me?
private void iconToBitmapToolStripMenuItem_Click(object sender, EventArgs e)
{
IUIX_Cmd cmd = uiInst.CmdManager.Cmds.Find("cmd.save");
IUIX_ImageData iData = null;
tagRECT rc;
cmd.Icon.GetItem(0, out iData, out rc);
Bitmap image = new Bitmap(rc.right - rc.left, rc.bottom - rc.top);
Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
System.Drawing.Imaging.BitmapData bmpData = image.LockBits(rect, ImageLockMode.ReadWrite, image.PixelFormat);
IntPtr outPtr = bmpData.Scan0;
byte[] rgbValues = new byte[bmpData.Stride];
uint nScan0 = (uint)(iData.Scan0 + iData.Stride * rc.top + rc.left * 4);
IntPtr ptr = new IntPtr(nScan0);
for (uint y = 0; y < (rc.bottom - rc.top); y++)
{
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, (rc.right - rc.left) * 4);
ptr = IntPtr.Add(ptr, iData.Stride);
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, outPtr, (rc.right - rc.left) * 4);
outPtr = IntPtr.Add(outPtr, bmpData.Stride);
}
image.UnlockBits(bmpData);
image.Save("D:\\TestFile.bmp");
}
User avatar
Vasyl-Tracker Dev Team
Site Admin
Posts: 2353
Joined: Thu Jun 30, 2005 4:11 pm
Location: Canada

Re: save icon to local system

Post by Vasyl-Tracker Dev Team »

Hi HomerWu.

Hope this will be helpful for you:

Code: Select all

IUIX_Cmd cmd = uiInst.CmdManager.Cmds.Find("cmd.save");
IUIX_IconItem iconItem = cmd.Icon.Item[0];
tagSIZE iconSize = iconItem.Size; // just initial size of vectorial source of icon
iconSize.cx = iconSize.cy = 128; // all our icons are vectorial so can be rasterized for any size

// rasterize to RGBA-bitmap in memory
IUIX_ImageData imageData = uiInst.CreateNewImage(iconSize.cx, iconSize.cy);
tagRECT rc;
rc.left = rc.top = 0;
rc.right = iconSize.cx; rc.bottom = iconSize.cy;
iconItem.DrawToImage(imageData, rc, rc);

// simple save to BMP
imageData.SaveToBmpFile("D:\\TestFile.bmp");

// extended method, to save in PNG or to many other formats, via our SDK
IIXC_Page imgPage = imageData.CreateIXCPage(rc);
imgPage.FmtInt[(uint)IXC_FormatParametersIDS.FP_ID_FORMAT] = (uint)IXC_ImageFileFormatIDs.FMT_PNG_ID;
IIXC_Image img = icInst.CreateEmptyImage();
img.InsertPage(imgPage);
img.Save("D:\\TestFile.png", IXC_CreationDisposition.CreationDisposition_Overwrite);

// Save via standard System.Drawing.Bitmap
Bitmap image = new Bitmap(rc.right - rc.left, rc.bottom - rc.top, imageData.Stride, PixelFormat.Format32bppArgb, (IntPtr)imageData.Scan0);
image.Save("D:\\TestFile2.png", ImageFormat.Png);
image.Save("D:\\TestFile2.png", ImageFormat.Bmp);
Cheers.
Vasyl Yaremyn
Tracker Software Products
Project Developer

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.