开发者

Save Bitmap image in a location in C#

开发者 https://www.devze.com 2023-01-04 11:19 出处:网络
I have this function to store the bmp image in a desired location as shown below My question is, how do I save the image in C:\\temp folder by default, instead of opening the filedialog box?

I have this function to store the bmp image in a desired location as shown below My question is, how do I save the image in C:\temp folder by default, instead of opening the filedialog box?

I want to specify sd.fileName=picname+".bmp" and store it in c:\temp by default.

I tried to specify

Thanks for your help in advance.

I tried to

public static bool SaveDIBAs( string picname, IntPtr bminfo, IntPtr pixdat )
{
        SaveFileDialog sd = new SaveFileDialog();

        sd.FileName = picname;
        s开发者_如何学Cd.Title = "Save bitmap as...";
        sd.Filter = "Bitmap file (*.bmp)|*.bmp|TIFF file (*.tif)|*.tif|JPEG file (*.jpg)|*.jpg|PNG file (*.png)|*.png|GIF file (*.gif)|*.gif|All files (*.*)|*.*";
        sd.FilterIndex = 1;
        if( sd.ShowDialog() != DialogResult.OK )
            return false;

        Guid clsid;
        if( ! GetCodecClsid( sd.FileName, out clsid ) )
            {
            MessageBox.Show( "Unknown picture format for extension " + Path.GetExtension( sd.FileName ),
                            "Image Codec", MessageBoxButtons.OK, MessageBoxIcon.Information );
            return false;
            }

        IntPtr img = IntPtr.Zero;
        int st = GdipCreateBitmapFromGdiDib( bminfo, pixdat, ref img );
        if( (st != 0) || (img == IntPtr.Zero) )
            return false;

        st = GdipSaveImageToFile( img, sd.FileName, ref clsid, IntPtr.Zero );
        GdipDisposeImage( img );
        return st == 0;
        }


If I understand you correctly, you do not want to use a SaveFileDialog any at all. Then I suggest you not to ;) . You are utilising SaveFileDialog to retrieve the path of the file which is SaveFileDialog.FileName in the line

st = GdipSaveImageToFile( img, sd.FileName, ref clsid, IntPtr.Zero );

Instead of sd.FileName, put the value you want; i.e, @"c:\temp\" + picname + ".bmp"

Remove anything that is related to SaveFileDialog.


You need to set the InitialDirectory of the SaveFileDialog to "C:\temp":

sd.FilterIndex = 1;
sd.InitialDirectory = @"C:\temp";  // <--- Add this line
if( sd.ShowDialog() != DialogResult.OK )
    return false;
0

精彩评论

暂无评论...
验证码 换一张
取 消