2012-02-03 2 views
1

매우 단순한 chequerboard 이미지가 있다고 상상해보십시오. 블랙 픽셀 'B', 백 화소 'W'여기서이미지 크기를 2x2 픽셀로 설정하면 4x4 픽셀로 색상이 동일하게 유지됩니다.

출발 × 2 픽셀 이미지이다 :

BW 
WB 

내가 리사이즈 할 여기

I 픽셀 같이 문자와 이미지를 나타내는

BBWW 
BBWW 
WWBB 
WWBB 

또는 규모의 4 배

BBBBWWWW 
BBBBWWWW 
WWWWBBBB 
WWWWBBBB 
0 : 나에게주는 2 배의 규모로 말할 수

픽셀 색상이 어떤 식 으로든 디더링되지 않도록하십시오.

나는 이것을 C#에서 시도했지만, 이미지가 엉망이며, 모두 디더링되었으므로 디더링/색상 변경없이이를 수행하는 방법을 배워야합니다.

여기에 지금까지 내 코드입니다 :

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Drawing; 
using System.Drawing.Imaging; 
using System.Drawing.Text; 

namespace PageTwine 
{ 
    public partial class RandonGiff : System.Web.UI.Page 
    { 


     protected void Page_Load(object sender, EventArgs e) 
     { 

      // create 2x2 chequerboard: 
      Bitmap oBitmap = new Bitmap(2, 2); 
      Graphics oGraphic = Graphics.FromImage(oBitmap); 

      // color black pixels (i think the default is black but this helps to explain) 
      SolidBrush oBrush = new SolidBrush(Color.FromArgb(255, 255, 255)); 
      oGraphic.FillRectangle(oBrush, 0, 0, 1, 1); 
      oGraphic.FillRectangle(oBrush, 1, 1, 1, 1); 

      //color white pixels 
      oBrush = new SolidBrush(Color.FromArgb(0, 0, 0)); 
      oGraphic.FillRectangle(oBrush, 0, 1, 1, 1); 
      oGraphic.FillRectangle(oBrush, 1, 0, 1, 1); 

      // expand to 4x4 
      Bitmap result = new Bitmap(4, 4); 
      using (Graphics graphics = Graphics.FromImage(result)) 
      { 
       // I don't know what these settings should be : 

       graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; 
       graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor; 
       graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None; 

       //draw the image into the target bitmap 
       graphics.DrawImage(oBitmap, 0, 0, result.Width, result.Height); 
      } 

      //send image directly to the browser 
      Response.ContentType = "image/gif"; 
      result.Save(Response.OutputStream, ImageFormat.Gif); 

     } 
    } 

} 

결과는 디더링 된 이미지입니다,하지만 난 깨끗하고 선명한 체커 효과가 필요합니다. 제발 도와주세요.

편집 : 제안 07/02/12

덕분에 지금까지하지만 난 아직 해결책을 찾지 못하고 검색하고 있습니다.

직접 결과를 볼 수 있도록 데모 페이지를 만들었습니다. 데모 용

URL은 :

http://www.leansoftware.net/servicedesk/publicadhoc/randomgif.aspx?columns=3&rows=3

후 데모 픽셀로서 초기 컬 X의 행이 체커를 만들 GIF 이미지 최대 300x300 픽셀로 확대한다.

색상이 왜곡되거나 비뚤어지는 것을 볼 수 있습니다.이 부분을 해결하려고합니다.

using System; 
using System.Collections.Generic; 
using System.Web; 
using System.IO; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Drawing; 
using System.Drawing.Imaging; 
using System.Drawing.Text; 
using System.Linq; 


    public partial class RandomGif : System.Web.UI.Page 
    { 
     public class Coordinates 
     { 
      public int x { get; set; } 
      public int y { get; set; } 
     } 

     static Random rand = new Random(); 

     protected void Page_Load(object sender, EventArgs e) 
     { 
      // Output type gif 
      int iwidth; 
      int iheight; 
      int bits; 
      Response.ContentType = "image/gif"; 

      // Get parameters 
      if (Request.QueryString["count"] != null) 
      { 
       bits = Convert.ToInt32(Request.QueryString["count"]); 
       int square = Convert.ToInt32(Math.Ceiling(Math.Sqrt(bits + Math.Floor(bits/4.0)))); 
       iwidth = square; 
       iheight = square; 
       bits = (square * square)-1; 
      } 
      else 
       if (Request.QueryString["rows"] != null && Request.QueryString["columns"] != null) 
       { 
        iwidth = Convert.ToInt32(Request.QueryString["columns"]); 
        iheight = Convert.ToInt32(Request.QueryString["rows"]); 
        bits = (iwidth * iheight); 
       } 
       else { 
        return; 
       } 
     if (bits > 1000){ 
     Response.Write("Please specify a grid <= 1000 pixels"); 
     return; 
     } 

      int plotCount = 0; 

      //web safe colors : 00, 33, 66, 99, CC, FF; 
      List<int> webSafeColor = new List<int>(); 
      webSafeColor.Add(0); //00 
      webSafeColor.Add(51);//33 
      webSafeColor.Add(102);//66 
      webSafeColor.Add(153);//99 
      webSafeColor.Add(204);//CC 
      webSafeColor.Add(255);//FF 

      // Create a structure to hold all possible coordinates 
      var Grid = new List<Coordinates>(); 
      for (int xi = 1; xi <= iwidth; xi++) 
      { 
       for (int yi = 1; yi <= iheight; yi++) 
       { 
        Grid.Add(new Coordinates { x = xi, y = yi }); 
        plotCount++; 
       } 
      } 

      //create a new Bitmap object 
      Bitmap oBitmap = new Bitmap(iwidth, iheight); 

      //create a new Graphics object, which will allow us to draw on our bitmap: 
      Graphics oGraphic = Graphics.FromImage(oBitmap); 


      //fill the image rectangle with n bits 
      for (int i = 1; i <= bits; i++) 
      { 
       //Random rand = new Random(); 
       int row = rand.Next(Grid.Count()); 

       // random red 
       int ircolor = webSafeColor[rand.Next(5)]; 

       // random green 
       int igcolor = webSafeColor[rand.Next(5)]; 

       // random blue 
       int ibcolor = webSafeColor[rand.Next(5)]; 

       Color randomColor = Color.FromArgb(ircolor, igcolor, ibcolor); 
       SolidBrush oBrush = new SolidBrush(randomColor); 
       oGraphic.FillRectangle(oBrush, Grid[row].x - 1, Grid[row].y - 1, 1, 1); 

     // Delete this coordinate# 
       Grid.Remove(Grid[row]); 
      } 

      // resize image 
      Bitmap result = new Bitmap(300, 300); 
      using (Graphics graphics = Graphics.FromImage(result)) 
      { 
       //set the resize quality modes to high quality 
       graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.AssumeLinear; 
       graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor; 
       graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; 
       graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half; 

       //draw the image into the target bitmap 
       graphics.DrawImage(oBitmap, 0, 0, result.Width, result.Height); 
      } 

      //send image directly to the browser 
      Response.ContentType = "image/gif"; 
      result.Save(Response.OutputStream, ImageFormat.Gif); 

     } 
    } 

당신이 수정을 제안 할 수 있다면 우리가 그들을 밖으로 시도 할 수 있으며이 문제가 해결되는지 확인 : 여기

소스 데모에 대한 코드입니다.

감사 리처드

답변

0

사용 :

System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor; 
+0

그것의 더 나은,하지만 이상한 결과 http://leansoftware.net/ServiceDesk/PublicAdHoc/result.png –

+0

사용 (그래픽 그래픽 = Graphics.FromImage (결과)) { // 나도 몰라 이 설정은 다음과 같아야합니다. //graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.AssumeLinear; graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor; //graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None; // 대상 비트 맵에 이미지를 그립니다. graphics.DrawImage (oBitmap, 0, 0, result.Width, result.Height); } –

+0

내가 추천 할 수있는 유일한 방법은 System.Drawing.Drawing2D.CompositingQuality.HighQuality를 사용하는 것입니다. – Brad

2

이 당신은 추가 할 필요가 c# Draw Image (Scaled) to Graphics, does not interpolate correctly. Fixes?

의 가능한 중복 :

graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half; 

다른 방법을 사용할 수

graphics.DrawImage(oBitmap, 0, 0, result.Width * 2, result.Height * 2); 

오른쪽 하단 이미지 가장자리에서 휘게하지 않고 브러시를 채우는 것과 유사한 효과를 얻을 수 있습니다.

업데이트 :

은 사용자 정의 색상 팔레트 인덱스 이미지를 만들기위한 링크를 추가. http://support.microsoft.com/kb/319061

+0

유감스럽게도 문제가 지속됩니다. 라이브 데모를 통해 어떤 일이 일어나는지 볼 수 있습니다 : 원래 질문의 업데이트를 참조하십시오. –

+0

image/png를 선택하면 어떻게됩니까? gif 이미지에서 사용하는 색상 표에 사용중인 색상이 포함되어 있지 않은 것처럼 보입니다. 원본 이미지를 그립니다.보고있는 효과를 디더링이라고합니다. –

+0

예! 성공했습니다. 짐 감사합니다.이 프로젝트의 최종 출력물은 다음과 같습니다. 애니메이션 GIF가 되어도 PNG 애니메이션이 모든 브라우저에서 지원되는 것은 아닙니다. 이제 색상 표에 사용 된 색상을 추가하는 방법을 알아야합니다. 어떻게해야하는지 알 수 있습니까? 다시 한 번 감사드립니다. –

관련 문제