简体   繁体   中英

Save bitmap as jpg to folder in pictures in windows 8 app c#

Ive been trying to save a bitmap as a jpeg into a folder named "temp" in the users picture folder

I ive been looking around the internet on how to go about doing this but havnt found any solutions, anyone have any solutions?

Try this

    static void Main(string[] args)
    {
        //choose my pictures/temp/file.jpg
        string target = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "temp", "file.jpg");
        //encode "test.bmp" to "my pictures/temp/file.jpg"
        EncodeToJPEG("test.bmp", target, 25);
    }

    public static void EncodeToJPEG(string sourceBitmap, string targetfile, long quality)
    {
        if(sourceBitmap == null)
            throw new ArgumentException("sourceBitmap");

        if(File.Exists(targetfile))
            throw new InvalidOperationException("target file already exists");

        var jpegCodec = ImageCodecInfo.GetImageEncoders().FirstOrDefault(x => x.MimeType == "image/jpeg");

        if (jpegCodec == null)
            throw new NotSupportedException();

        var jpegParams = new EncoderParameters(1);
        jpegParams.Param[0] = new EncoderParameter(Encoder.Quality, quality);

        var bitmap = new Bitmap(sourceBitmap);

        var dir = Path.GetDirectoryName(targetfile);
        if (dir != null && !Directory.Exists(dir))
            Directory.CreateDirectory(dir);



        bitmap.Save(targetfile, jpegCodec, jpegParams);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM