그냥은 안되고 외부 라이브러리를 사용해야 합니다.
FluxJpeg라는 라이브러리인데 비트맵을 JPG로 변환할때 사용합니다.
using System.Windows.Media.Imaging;
using System.IO;
public class claExportImg
{
Canvas m_layoutScreen = null;
public claExportImg()
{
}
public void OnCapture(Canvas layoutScreen)
{
//사용할 레이아웃 지정
this.m_layoutScreen = layoutScreen;
WriteableBitmap wb = new WriteableBitmap(this.m_layoutScreen, null);
//이미지 객체
Image imgTemp = new Image();
imgTemp.Width = Convert.ToInt32(this.m_layoutScreen.ActualWidth);
imgTemp.Height = Convert.ToInt32(this.m_layoutScreen.ActualHeight);
imgTemp.Source = wb;
//최종 이미지 파일 이름
string strFileName = SaveImage(imgTemp) + ".JPG";
}
private string SaveImage(Image imgObject)
{
string sReturn = "";
//세이브 파일 다얄로그
SaveFileDialog sfdFile = new SaveFileDialog();
//라이브러리가 제이팩뿐이 없으니 파일 다얄로그에 jpg만 표시한다.
sfdFile.Filter = "JPEG File | *.jpg";
//사용자가 파일 지정했는지?
if (sfdFile.ShowDialog() == true)
{
//파일 지정을 했다.
//저장할 파일을 오픈하고
//여기서 using를 사용하지 않으면 실버라이트의 제한사항인 사용자가 직접 호출하지 않았다는 오류가 발생합니다.
using (Stream stream = sfdFile.OpenFile())
{
//데이터를 넣고
ImageWriteStream(imgObject, stream);
//세이브 파일 다얄로그가 생성한 파일이름은 확장자같은 것들이 붙을수 있으니 다시 준다.
sReturn = sfdFile.SafeFileName;
//파일을 닫아줍니다.
stream.Close();
}
}
return sReturn;
}
/// <summary>
/// 이미지를 스트림으로 변경
/// </summary>
/// <param name="img"></param>
/// <param name="st"></param>
private static void ImageWriteStream(Image img, Stream st)
{
System.Windows.Media.Imaging.WriteableBitmap bitmap = new System.Windows.Media.Imaging.WriteableBitmap(img, null);
//FJ객체 초기화
FluxJpeg.Core.ColorModel cmModel = new FluxJpeg.Core.ColorModel();
cmModel.Opaque = true;
cmModel.colorspace = FluxJpeg.Core.ColorSpace.RGB;
//칼라 배열
byte[][,] byteColor = FluxJpeg.Core.Image.CreateRaster(bitmap.PixelWidth, bitmap.PixelHeight, 3);
for (int y = 0; y < bitmap.PixelHeight; y++)
{
int nWidth = y * bitmap.PixelWidth;
for (int x = 0; x < bitmap.PixelWidth; x++)
{
int color = bitmap.Pixels[nWidth + x];
//비트연산으로 알쥐비값을 받아온다.
//Red
byteColor[0][x, y] = (byte)((color >> 16) & 0xff);
//Green
byteColor[1][x, y] = (byte)((color >> 8) & 0xff);
//Blue
byteColor[2][x, y] = (byte)(color & 0xff);
}
}
// 이미지 파일 인코딩
FluxJpeg.Core.Image saveimg = new FluxJpeg.Core.Image(cmModel, byteColor);
new FluxJpeg.Core.Encoder.JpegEncoder(saveimg, 100, st).Encode();
}
}