void appBarIconBtnSave_Click(object sender, EventArgs e) { try { using (MemoryStream ms = new MemoryStream()) { //이미지 로드 WriteableBitmap wb = ImgDetail.Source as WriteableBitmap; wb.SaveJpeg(ms, wb.PixelWidth, wb.PixelHeight, 0, 100); //마지막 항목 사진중심으로 잘라내기는 사용하지 않음 //JpegHelper.Save(NavigationContext.QueryString["imgName"], wb, ms, false); string name = NavigationContext.QueryString["imgName"]; //축소 가능한 이미지라면 축소 JpegHelper.Resize(wb, ms, ResolutionHelper.CurrentResolution, false); //이미지 파일 저장 FileHelper.SaveImage(name, ms); //썸네일 만들기 (이미지의 중심 비율등이 변경될 수 있으므로 재생성해서 덮어쓰기) JpegHelper.Resize(wb, ms, LockscreenHelper.ThumnailSize, true); //썸네일 저장 FileHelper.SaveImage(name.Replace(Constants.LOCKSCREEN_IMAGE_POSTFIX, Constants.LOCKSCREEN_IMAGE_THUMNAIL_POSTFIX), ms); //현재 프레임으로 자르기 WriteableBitmap cropImage = GetCropImage(); //락스크린용 이미지로 축소 또는 확대 JpegHelper.Resize(cropImage, ms, LockscreenHelper.Size, false); //락스크린 파일 저장 FileHelper.SaveImage(name.Replace(Constants.LOCKSCREEN_IMAGE_POSTFIX, Constants.LOCKSCREEN_IMAGE_READY_POSTFIX), ms); } MessageBox.Show(AppResources.MsgSuccessSaveImage); } catch (Exception ex) { MessageBox.Show(string.Format(AppResources.MsgFailSaveImage, ex.Message)); } }
void bgPhoneLoader_ProgressChanged(object sender, ProgressChangedEventArgs e) { object[] args = e.UserState as object[]; ChameleonAlbum phoneAlbum = args[0] as ChameleonAlbum; Picture picture = args[1] as Picture; using (Stream stream = picture.GetImage()) { WriteableBitmap bitmap = JpegHelper.Resize(stream, new Size(200, 200), true); phoneAlbum.Add(new PhonePicture() { SourceOrigin = SourceOrigin.Phone, AlbumName = picture.Album.Name, ImageSource = bitmap, Name = picture.Name }); } }
private bool Resize(WriteableBitmap wb, Stream stream, Size size, bool isCenterCrop, DownloadItem item, ManualResetEvent done) { bool isResized = false; done.Reset(); Dispatcher.BeginInvoke(() => { try { isResized = JpegHelper.Resize(wb, stream, size, isCenterCrop); } catch (Exception ex) { item.DownloadStatusCode = DownloadStatus.SaveFailed; System.Diagnostics.Debug.WriteLine(ex.Message); } finally { done.Set(); } }); done.WaitOne(); return(isResized); }
private void SetLockscreen(PhonePicture picture, WriteableBitmap wb, Stream beforeImageStream) { string fileName = picture.Name.Replace(Constants.LOCKSCREEN_IMAGE_POSTFIX, Constants.LOCKSCREEN_IMAGE_A_POSTFIX); Uri currentImage = null; try { currentImage = LockScreen.GetImageUri(); } catch (Exception) { MessageBox.Show(AppResources.MsgFailGetLockscreen); HideLoadingPanel(); return; } if (currentImage != null && currentImage.ToString().EndsWith(Constants.LOCKSCREEN_IMAGE_A_POSTFIX)) { fileName = picture.Name.Replace(Constants.LOCKSCREEN_IMAGE_POSTFIX, Constants.LOCKSCREEN_IMAGE_B_POSTFIX); } using (MemoryStream ms = new MemoryStream()) { //축소/확대 가능한 이미지라면 축소/확대 JpegHelper.Resize(wb, ms, ResolutionHelper.CurrentResolution, true); FileHelper.SaveImage(fileName, ms); LockscreenHelper.SetLockscreen(fileName, false, (result) => { if (result.AsyncState is string) { MessageBox.Show(AppResources.MsgFailChangeLockscreen); } else if (result.AsyncState is bool && (bool)result.AsyncState == true) { if (currentImage != null && currentImage.ToString().StartsWith(Constants.PREFIX_APP_DATA_FOLDER)) { fileName = currentImage.ToString().Replace(Constants.PREFIX_APP_DATA_FOLDER, string.Empty); FileHelper.RemoveImage(fileName); } for (int i = 0; i < LockscreenSelector.ItemsSource.Count; i++) { PhonePicture pic = (LockscreenSelector.ItemsSource as ChameleonAlbum)[i] as PhonePicture; if (pic.CurrentLockscreen != null) { pic.CurrentLockscreen = null; pic.Margin = new Thickness(); } //자기자신으로 변경한 경우 및 새롭게 변경된 아이템 if (pic.Guid.CompareTo(picture.Guid) == 0) { pic.CurrentLockscreen = currentLockscreenUri; pic.Margin = currentLockscreenMargin; } } //표시항목들이 적용되지 않은 잘라내기가 수행된 이미지 데이터 if (picture.Warnning != null && beforeImageStream != null) { if (MessageBox.Show(AppResources.MsgEditedAutomatically, AppResources.Confirm, MessageBoxButton.OKCancel) == MessageBoxResult.OK) { //ready 파일 저장 //락스크린용 이미지로 축소 WriteableBitmap rwb = JpegHelper.Resize(beforeImageStream, LockscreenHelper.Size, true); using (MemoryStream rms = new MemoryStream()) { rwb.SaveJpeg(rms, rwb.PixelWidth, rwb.PixelHeight, 0, 100); FileHelper.SaveImage(picture.Name.Replace(Constants.LOCKSCREEN_IMAGE_POSTFIX, Constants.LOCKSCREEN_IMAGE_READY_POSTFIX), rms); } //상태 변경 picture.Warnning = null; //경고 메세지 제어 bool isShowWarn = false; foreach (PhonePicture pic in LockscreenSelector.ItemsSource as ChameleonAlbum) { if (pic.Warnning != null) { isShowWarn = true; break; } } LockscreenEditWarnning = isShowWarn; SetSchedulerChagendTime(false); //MessageBox.Show(AppResources.MsgSuccessChangeLockscreen); } } else { SetSchedulerChagendTime(false); //MessageBox.Show(AppResources.MsgSuccessChangeLockscreen); } if (beforeImageStream != null) { //스트림 종료 beforeImageStream.Close(); } } HideLoadingPanel(); }); } }
//락스크린 리스트 로딩 public void LoadLockscreenList() { //락스크린 썸네일 이미지 크기 설정 LockscreenSelector.GridCellSize = LockscreenHelper.ThumnailSize; ChameleonAlbum phoneAlbum = LockscreenSelector.ItemsSource as ChameleonAlbum; if (phoneAlbum == null) { phoneAlbum = new ChameleonAlbum(); LockscreenSelector.ItemsSource = phoneAlbum; } using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication()) { var imgNames = from element in isoStore.GetFileNames() where element.Contains(Constants.LOCKSCREEN_IMAGE_POSTFIX) orderby element.Substring(0, element.IndexOf("_")) ascending select element; Uri lockscreenFileUri = null; try { lockscreenFileUri = LockScreen.GetImageUri(); } catch (Exception) { } string lockscreenFileName = null; if (lockscreenFileUri != null) { lockscreenFileName = lockscreenFileUri.ToString() .Replace(Constants.PREFIX_APP_DATA_FOLDER, string.Empty) .Replace(Constants.LOCKSCREEN_IMAGE_A_POSTFIX, Constants.LOCKSCREEN_IMAGE_POSTFIX) .Replace(Constants.LOCKSCREEN_IMAGE_B_POSTFIX, Constants.LOCKSCREEN_IMAGE_POSTFIX); } foreach (string imgName in imgNames) { bool isReady = isoStore.GetFileNames(imgName.Replace(Constants.LOCKSCREEN_IMAGE_POSTFIX, Constants.LOCKSCREEN_IMAGE_READY_POSTFIX)).Any(); var pic = from element in phoneAlbum where element.Name == imgName select element; if (pic.Any()) { //원래 존재하는 이름이므로 해당 파일에 대한 정보를 업데이트 처리 PhonePicture curPic = pic.First() as PhonePicture; if (curPic.Name == lockscreenFileName) { curPic.CurrentLockscreen = currentLockscreenUri; curPic.Margin = currentLockscreenMargin; } else { curPic.CurrentLockscreen = null; curPic.Margin = new Thickness(); } //이미지 변경 시간을 체크해서, 이미지가 편집이 되었다면 이미지를 다시 로드 DateTimeOffset offset = isoStore.GetLastWriteTime(imgName); if (offset.Subtract(curPic.DateTimeOffset).Milliseconds != 0) { curPic.DateTimeOffset = offset; WriteableBitmap bitmap = null; string thumbName = imgName.Replace(Constants.LOCKSCREEN_IMAGE_POSTFIX, Constants.LOCKSCREEN_IMAGE_THUMNAIL_POSTFIX); string[] thumbs = isoStore.GetFileNames(thumbName); //썸네일이 없는 경우면 원본 이름을 셋팅 if (thumbs == null || thumbs.Length == 0) { thumbName = imgName; } //이미지 로드 using (IsolatedStorageFileStream sourceStream = isoStore.OpenFile(thumbName, FileMode.Open, FileAccess.Read)) { //썸네일이 없는 경우면 원본을 리사이징 if (thumbs == null || thumbs.Length == 0) { bitmap = JpegHelper.Resize(sourceStream, LockscreenHelper.ThumnailSize, true); } else { bitmap = BitmapFactory.New(0, 0).FromStream(sourceStream); } } //썸네일 이미지 교체 curPic.ThumbnailImageSource = bitmap; } //편집 페이지에서 준비상태로 편집이 완료되었으면 편집 경고 삭제 if (isReady) { curPic.Warnning = null; } } else { //존재하지 않는 파일이므로 새롭게 리스트에 추가 WriteableBitmap bitmap = null; string thumbName = imgName.Replace(Constants.LOCKSCREEN_IMAGE_POSTFIX, Constants.LOCKSCREEN_IMAGE_THUMNAIL_POSTFIX); string[] thumbs = isoStore.GetFileNames(thumbName); Uri uri = null; Thickness margin = new Thickness(); //썸네일이 없는 경우면 원본 이름을 셋팅 if (thumbs == null || thumbs.Length == 0) { thumbName = imgName; } //이미지 로드 using (IsolatedStorageFileStream sourceStream = isoStore.OpenFile(thumbName, FileMode.Open, FileAccess.Read)) { //썸네일이 없는 경우면 원본을 리사이징 if (thumbs == null || thumbs.Length == 0) { bitmap = JpegHelper.Resize(sourceStream, LockscreenHelper.ThumnailSize, true); } else { bitmap = BitmapFactory.New(0, 0).FromStream(sourceStream); } } //현재 락스크린 지정 이미지인 경우 처리 if (lockscreenFileName == imgName) { uri = currentLockscreenUri; margin = currentLockscreenMargin; } //락스크린 이미지 객체 생성 phoneAlbum.Add(new PhonePicture() { Guid = Guid.NewGuid(), Name = imgName, ThumnailName = thumbName, ThumbnailImageSource = bitmap, Margin = margin, CurrentLockscreen = uri, Warnning = isReady ? null : warnningUri, Opacity = 1, DateTimeOffset = isoStore.GetLastWriteTime(thumbName) }); } //경고 안내문구 표시 if (!LockscreenEditWarnning && !isReady) { LockscreenEditWarnning = true; } } } (IAppBarLockscreen.Buttons[0] as ApplicationBarIconButton).IsEnabled = !(phoneAlbum.Count == 0 && IAppBarLockscreen.Buttons.Count > 1); //이미지 갯수 표시 LockscreenCount = string.Format("({0})", phoneAlbum.Count); //에디팅 경고 표시 LockscreenEditWarnning = phoneAlbum.Any(x => (x as PhonePicture).Warnning != null); if (phoneAlbum.Count == 0) { //도움말 표시 토글 TxtLockscreen.Visibility = Visibility.Visible; //이미지가 없고 활성화된 라이브타일이 없으면 스케쥴러 정저 if (!ExistsActiveTile) { RemoveAgent(Constants.PERIODIC_TASK_NAME); } UseLockscreen.IsChecked = false; UseLockscreen.IsEnabled = false; } else { UseLockscreen.IsEnabled = true; TxtLockscreen.Visibility = Visibility.Collapsed; } }