Expand canvas/transparent background in BitmapImage in a WPF app

后端 未结 3 2129
无人及你
无人及你 2021-02-09 13:23

This is a follow up question on Save image to file keeping aspect ration in a WPF app

I know howto scale the image, but how do I expand the canvas size, to ensure the im

3条回答
  •  温柔的废话
    2021-02-09 13:55

    Just some code in case others are trying to postprocess files from a form upload.

    if (file.PostedFile != null)
    {
        //write the new file to disk
        string cachePath = String.Format("{0}temp\\", Request.PhysicalApplicationPath);
        string photoPath = String.Format("{0}temp.png", cachePath);                
        if (!Directory.Exists(cachePath))
    {
       Directory.CreateDirectory(cachePath);
    }    
    
    file.PostedFile.SaveAs(photoPath);
    
    //resize the new file and save it to disk               
    BitmapSource banana = FitImage(ReadBitmapFrame(file.PostedFile.InputStream), 640, 480);
    PngBitmapEncoder encoder = new PngBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(banana));
    FileStream pngStream = new FileStream(cachePath + "test.png", FileMode.Create);
    encoder.Save(pngStream);
    pngStream.Close();
    
    //set a couple images on page to the newly uploaded and newly processed files
    image.Src = "temp/temp.png";
    image.Visible = true;
    image2.Src = "temp/test.png"; 
    
    
       image2.Visible = true;
    }
    

提交回复
热议问题